javascript - hide dataset by default using Chart.js and a custom script - Stack Overflow

I'm using wpDataTables plugin on my WordPress website to draw some tables and charts. The charts a

I'm using wpDataTables plugin on my WordPress website to draw some tables and charts. The charts are rendered using Chart.js. The developers of the plugin created a custom script to control some more aspects of Chart.js than the plugin can handle out of the box.

That's the script:

<script type="text/javascript">
jQuery(window).load(function(){
    if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; }
    wpDataChartsCallbacks[39] = function(obj){
        obj.options.options.scales.yAxes[0].display = false;
        obj.options.options.scales.xAxes[0].display = false;
    }
});
</script>

In this example it hides the axes of the chart with the ID 39. I need to change the above script to hide a specific dataset (line in the chart) by default. My chart ID is 2 and I want to hide the dataset labelled "SPM".

You can take a look at the table and chart how it currently looks like here (it's a weapon overview of a game, so please do not wonder): /

The first chart has all datasets visible by default: I want it to look like this by default ("SPM" line hidden):

Can anyone please assist how to do that?

I'm using wpDataTables plugin on my WordPress website to draw some tables and charts. The charts are rendered using Chart.js. The developers of the plugin created a custom script to control some more aspects of Chart.js than the plugin can handle out of the box.

That's the script:

<script type="text/javascript">
jQuery(window).load(function(){
    if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; }
    wpDataChartsCallbacks[39] = function(obj){
        obj.options.options.scales.yAxes[0].display = false;
        obj.options.options.scales.xAxes[0].display = false;
    }
});
</script>

In this example it hides the axes of the chart with the ID 39. I need to change the above script to hide a specific dataset (line in the chart) by default. My chart ID is 2 and I want to hide the dataset labelled "SPM".

You can take a look at the table and chart how it currently looks like here (it's a weapon overview of a game, so please do not wonder): https://mydivision/the-division-waffen/

The first chart has all datasets visible by default: I want it to look like this by default ("SPM" line hidden):

Can anyone please assist how to do that?

Share Improve this question asked Mar 1, 2018 at 15:32 jackennilsjackennils 3031 gold badge3 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

This is taken from the ChartJS GitHub.

You set the new value and then use the ChartJS API to update the chart to hide the ones you have toggled off. If you wanted to toggle them again you just do the reverse.

//Hide
chart.getDatasetMeta(1).hidden=true;
chart.update();

//Show
chart.getDatasetMeta(1).hidden=false;
chart.update();

Here's an example in JSFiddle.

you just need to add hidden:true to your datasets

{
  label: "something",
  backgroundColor: 'green',
  data: data,
  hidden: true
}

The solution is:

<script type="text/javascript">
jQuery(window).load(function(){
    if( typeof wpDataChartsCallbacks == 'undefined' ){ wpDataChartsCallbacks = {}; }
    wpDataChartsCallbacks[2] = function(obj){
        obj.options.data.datasets[5].hidden = true;
    }
});
</script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743638599a4482574.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信