вторник, 2 сентября 2025 г.

25.09.02, RazorzzSln, Sine, Cycle, RazorPage, Singletone, State, Render, PartialView, Work well, From Giga

25.09.02, RazorzzSln, Sine, Cycle, RazorPage, Singletone, State, Render, PartialView, Work well,
From Giga  - Exelent Project
BEST CODE
F:\Projects\VS\RazorzzSln\RazorzzSln.sln
F:\Projects\VS\RazorzzSln\RazorSine05\RazorSine05.csproj WORK WELL
------------------------------------------------------------------------------------------------------------

SimpleLine.cshtml.cs

-----------------------------------------------------------------------------------------------------------
using ChartDirector;
using LinSpace;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorChartDirApp07.Services;

namespace RazorChartDirApp07.Pages
{
    public class SimpleLineModel : PageModel
    {
        private readonly IRunningSineState _state;
        private readonly ILogger<SimpleLineModel> _logger;

        public SimpleLineModel(IRunningSineState state, ILogger<SimpleLineModel> logger)
        {
            _state = state;
            _logger = logger;
        }

        public void OnGet()
        {
            ViewData["Title"] = "Simple Line Chart";
            RazorChartViewer viewer = new RazorChartViewer(HttpContext, "chart1");
            ViewData["Viewer"] = viewer;
            createChart(viewer);
        }

        private void createChart(RazorChartViewer viewer)
        {
            var dataArg = MathSeries.Linspace(0.0, 2 * Math.PI, 36, _state.CurrentIndex);
            var data = MathSeries.Evaluate(dataArg, Math.Sin);
            var nextIndex = _state.NextIndex(36);
            
            string[] labels = Enumerable.Range(0, 36).Select(i => i.ToString()).ToArray();

            XYChart c = new XYChart(450, 450);
            c.setPlotArea(30, 20, 400, 400);
            c.addLineLayer(data);
            c.xAxis().setLabels(labels);
            c.xAxis().setLabelStep(3);

            viewer.Image = c.makeWebImage(Chart.SVG);
            viewer.ImageMap = c.getHTMLImageMap("", "",
                "title='Hour {xLabel}: Traffic {value} GBytes'");
        }
    }
}
-----------------------------

SimpleLine.cshtml

-----------------------------
@page
@model RazorChartDirApp08.Pages.SimpleLineModel
@using ChartDirector
@{
    ViewData["Title"] = "Simple Line Chart";
    var initialHtml = ((RazorChartViewer)ViewData["Viewer"]).RenderHTML();
}

<h2>@ViewData["Title"]</h2>

<div id="chart-container">
    @Html.Raw(initialHtml)
</div>

<script>
    (function(){
        const container = document.getElementById('chart-container');
        
        async function refreshChart() {
            try {
                const resp = await fetch('@Url.Page("SimpleLine", "ChartHtml")', {
                    method: 'GET',
                    headers: { 'Accept': 'text/html' },
                    cache: 'no-store'
                });
                
                if (!resp.ok) return;
                const html = await resp.text();
                container.innerHTML = html;
            } catch(e) {}
        }
    
        const intervalMs = 1000;
        setInterval(refreshChart, intervalMs);
    })();
</script>
-----------------------------------------------------------------

Комментариев нет:

Отправить комментарий