简体   繁体   English

Chart JS未在部分视图中重新加载

[英]Chart JS not reloading in Partial View

I am using Chart JS. 我正在使用Chart JS。 http://www.chartjs.org/docs/ http://www.chartjs.org/docs/

in MVC C# I have a Partial View with Data and Graph: 在MVC C#中,我具有带有数据和图形的局部视图:

<div style="width: 700px; height: 450px; float: left">
    <canvas id="fuelGraph" style="width: 700px; height: 450px;"></canvas>
</div>



<script type="text/javascript">
   var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var items = @Html.Raw(Json.Encode(fuelValues));
    var itemLabel = @Html.Raw(Json.Encode(dateValues));
    console.log(items);
    console.log(itemLabel);

    var lineChartData = {
        labels: itemLabel,
        datasets: [

            {
                label: "My Second dataset",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: items
            }
       ]

    }

    $(document).ready(function() {
        var ctx = document.getElementById("fuelGraph").getContext("2d");

        window.myLine = new Chart(ctx).Line(lineChartData, {
            responsive: true
        });

    });
</script>

When I load this Partial View the First Time the Chart Loads as expected: 当我第一次加载此局部视图时,图表将按预期加载:

在此处输入图片说明

But when I load this partial view again after closing partial view the Canvas is Blank: 但是,当我在关闭局部视图后再次加载此局部视图时,“画布”为空白:

There are no Errors in Console. 控制台中没有错误。 and I am loading this Partial View in Modal Dialogue. 我正在模式对话中加载此局部视图。

EDIT: 编辑:

This is the Partial View: 这是部分视图:

@model xPTCommon.DTO.AssetsDTO.FuelReadingsDTO


@{
    var fuelValues = Model.Readings.Select(s => s.FuelReading).ToArray();
    var dateValues = Model.Readings.Select(s => s.ReadingDateTime.ToString("dd/MM/yyyy HH:mm")).ToArray();
}
<div id="fuelGraphArea" style="width: 700px; height: 450px; float: left">
    <canvas id="fuelGraph" style="width: 700px; height: 450px;"></canvas>
</div>

<script type="text/javascript">
    var customLine;

    var monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var items = @Html.Raw(Json.Encode(fuelValues));
    var itemLabel = @Html.Raw(Json.Encode(dateValues));

    var lineChartData = {
        labels: itemLabel,
        datasets: [

            {
                label: "My Second dataset",
                fillColor: "rgba(151,187,205,0.2)",
                strokeColor: "rgba(151,187,205,1)",
                pointColor: "rgba(151,187,205,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(151,187,205,1)",
                data: items
            }
        ]

    }

    $(document).ready(function() {

        $('#fuelGraph').remove(); // this is my <canvas> element
        $('#fuelGraphArea').append('<canvas id="fuelGraph"><canvas>');
        var ctx = document.getElementById("fuelGraph").getContext("2d");
        ctx.canvas.width = 700; 
        ctx.canvas.height = 450;

        customLine = new Chart(ctx).Line(lineChartData, {
            responsive: true
        });
    });
</script>

Controller: This is the method I call from Ajax, 控制器:这是我从Ajax调用的方法,

public ActionResult AssetFuelDisplay(int assetId, int companyId)
{
    var model = DALProvider.GetFuelReadings(assetId, companyId);

    return PartialView(model);
}

And it is a simple Ajax Request: 这是一个简单的Ajax请求:

$.ajax({

method: 'GET',
url: @Url.Action("AssetFuelDisplay", "Controller"),
success: function(data){

$('partial-view').html(data);
$('partial-view').ShowModal();
}
})

Try to initialize the chart object when opening the jQueryUI dialog, and .destroy() it on the closing dialog event. 尝试在打开jQueryUI对话框时初始化图表对象,并在关闭对话框事件时对其进行.destroy()
Also you can try setting maintainAspectRatio: false if it still doesn't work the second time. 您也可以尝试设置maintainAspectRatio: false如果第二次仍然不起作用,则为maintainAspectRatio: false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM