简体   繁体   English

如何使用mvc c#从鼠标悬停时的Chart js中删除旧数据?

[英]how to remove old data from Chart js on mouse hover using mvc c#?

I am create one chart module in my project. 我在项目中创建一个图表模块。 I have add filter on this charts like month wise show data and week wise show data. 我在此图表上添加了筛选器,例如按月显示数据和按周显示数据。 and filter also working very well.but i have facing one issue on this charts. 和过滤器也很好用。但是我在这张图表上遇到了一个问题。 i have no idea where is my mistake or some js is coming I haven't idea. 我不知道我的错误在哪里或一些js来了我不知道。

Here I am describing my problem- First time load the charts month wise. 我在这里描述我的问题-每月一次加载图表。 then after I have change drop down value and select week wise show data. 然后在我更改之后,下拉值并选择每周明智的显示数据。 then also data is show correct. 然后数据也显示正确。 but then after I am going on this chart with my mouse pointer and pointer move left to right that time chart data is change and automatically show month data . 但是然后当我用鼠标指针移至该图表时,指针从左移至右表示时间图表数据改变并自动显示月数据 why this problem is getting I don't know. 为什么这个问题变得我不知道。 here below i have show my code how can do that this task. 在下面的代码中,我显示了我的代码如何执行此任务。 and please let me know where is my problem. 请让我知道我的问题在哪里。

Here is my html page => 这是我的html页面=>

<canvas id="myChart"></canvas>

This is my filter drop down => 这是我的过滤器下拉菜单=>

  Filter :
            <select id="ddlFilter" onchange="FilterCharts()">
                <option selected="" value="Month">Month</option>
                <option value="Week">Week</option>
            </select>

This is my script link => 这是我的脚本链接=>

<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.4/Chart.min.js"></script>

This is my ajax call on first time load intitally month wise => 这是我第一次对ajax进行初始加载,月初=>

var FilterType = '';
$(document).ready(function () { 
  FilterType = $("#ddlFilter option:selected").val();
   LoadCharts(FilterType );
 });

This is my ajax call and bind data in chart => 这是我的ajax调用,并在图表中绑定数据=>

function LoadCharts(FilterType )
{
$.ajax({
    url: 'Home/GetChartsData',
    dataType: "json",
    data: { FilterType: FilterType},
    type: "GET",
    contentType: 'application/json; charset=utf-8',
    async: false,
    cache: false,
    success: function (data) {
         var arrlableForSignUpUser = new Array();
        var arrdataForSignUpUser = new Array();
        for (var i = 0; i < data.data.length; i++) {
            arrlableForSignUpUser.push(data.data[i].DateColumn);
            arrdataForSignUpUser.push(data.data[i].TotalCount);
        }
        CountSignUpUser(arrlableForSignUpUser, arrdataForSignUpUser);
    },
    error: function (xhr) {
        alert('error');
    }
});
}

function CountSignUpUser(arrlableForSignUpUser, arrdataForSignUpUser) {
var ctx = document.getElementById("myChart").getContext('2d');
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: arrlableForSignUpUser,
        datasets: [{
            label: 'Successfull Sign-Ups',
            data: arrdataForSignUpUser,
            //backgroundColor: "rgba(153,255,51,1)"
            backgroundColor: '#34999c'
        }]
    },
});
}

This is my filter call for bind data week wise => 这是我每周对绑定数据的过滤器调用=>

function FilterCharts() { 
   FilterType = $("#ddlFilter option:selected").val();       
    LoadCharts(FilterType );
}

using this code I have success for the show chart.but here I am getting problem this describe code above. 使用此代码,我可以成功显示图表。但是在这里,我遇到了上面描述代码的问题。 so please help me where is my problem. 所以请帮助我我的问题在哪里。

I am getting solution on this post my self just need to destroy the myChart variable every time new chart create. 我正在此帖子上找到解决方案,每次创建新图表时,我只需要销毁myChart变量即可。 and also declare the globally variable so esially access. 并声明全局变量以方便访问。

Like this declare globally varibale in js => 像这样在js中声明全局varibale =>

var myChart = '';

and just check like this condition on create chart time => 然后在创建图表时间时检查这种情况=>

if (myChart) myChart.destroy();

so using this condition every time your chart is create fresh and remove all old data. 因此,每次您的图表重新创建并删除所有旧数据时,请使用此条件。

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

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