首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >隐藏CanvasJS图形中的所有系列按钮(javascript的用法)

隐藏CanvasJS图形中的所有系列按钮(javascript的用法)
EN

Stack Overflow用户
提问于 2018-06-06 04:06:12
回答 2查看 418关注 0票数 0

这个问题与CanvasJS有关,但可能任何纯javascript专家都可以提供帮助。

我需要一个按钮来隐藏/取消隐藏canvasjs图中的所有元素。有一个可以使用数组索引隐藏元素的有效代码:

代码语言:javascript
复制
chart.options.data[0].visible = !chart.options.data[0].visible;

我试图通过数组,但它不工作,显然我的代码是错误的:

代码语言:javascript
复制
chart.options.data.forEach(visible = !visible);

我该怎么解决它呢?

完整的代码片段如下:

代码语言:javascript
复制
 var chart = new CanvasJS.Chart("chartContainer",
    {
      title:{
      text: "Test Button to Hide All Series"  
      },
      
      legend: {
            cursor: "pointer",
            itemclick: function (e) {
                //console.log("legend click: " + e.dataPointIndex);
                //console.log(e);
                if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                    e.dataSeries.visible = false;
                } else {
                    e.dataSeries.visible = true;
                }
 
                e.chart.render();
            }
        },
      
      data: [
      { 
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 5 },
        { x: 20, y: 9},
        { x: 30, y: 17 },
        { x: 40, y: 32 },
        { x: 50, y: 22 },
        { x: 60, y: 14 },
        { x: 70, y: 25 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 31 },
        { x: 20, y: 35},
        { x: 30, y: 30 },
        { x: 40, y: 35 },
        { x: 50, y: 35 },
        { x: 60, y: 38 },
        { x: 70, y: 38 },
        { x: 80, y: 34 },
        { x: 90, y: 44}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 25 },
        { x: 20, y: 30},
        { x: 30, y: 20 },
        { x: 40, y: 45 },
        { x: 50, y: 30 },
        { x: 60, y: 10 },
        { x: 70, y: 15 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      }
      ]
    });
 
    chart.render();
    
 document.getElementById("showAllSeries").onclick =  function(){
   //Works for a single element using index:
   chart.options.data[0].visible = !chart.options.data[0].visible;
   //Doesn't work, need to modify
   //chart.options.data.forEach(visible = !visible);
   chart.render();
 };
代码语言:javascript
复制
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<center><button id= "showAllSeries" style= "margin: 10px;">Show/Hide All series</button></center>

UP:找到了使用for循环的解决方案:

代码语言:javascript
复制
   for (var i = 0; i < chart.options.data.length; i++) {
   chart.options.data[i].visible = !chart.options.data[i].visible;
   }

但仍然很有趣,它应该如何与foreach一起工作

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-06 12:43:02

forEach是一个数组方法,可以用来对数组中的每个元素执行函数。另一方面,for是一个更灵活的循环。

在您的例子中,您可以使用for或forEach隐藏按钮的所有dataSeries onclick。检查下面的代码:

代码语言:javascript
复制
var chart = new CanvasJS.Chart("chartContainer", {
	title:{
		text: "Test Button to Hide All Series"  
	},

	legend: {
		cursor: "pointer",
		itemclick: function (e) {
			if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
				e.dataSeries.visible = false;
			} else {
				e.dataSeries.visible = true;
			}
			e.chart.render();
		}
	},

	data: [
		{ 
		showInLegend: true,
		type: "line",
		dataPoints: [
			{ x: 10, y: 5 },
			{ x: 20, y: 9 },
			{ x: 30, y: 17 },
			{ x: 40, y: 32 },
			{ x: 50, y: 22 },
			{ x: 60, y: 14 },
			{ x: 70, y: 25 },
			{ x: 80, y: 18 },
			{ x: 90, y: 20 }
		]
		},
		{
		showInLegend: true,
		type: "line",
		dataPoints: [
			{ x: 10, y: 31 },
			{ x: 20, y: 35 },
			{ x: 30, y: 30 },
			{ x: 40, y: 35 },
			{ x: 50, y: 35 },
			{ x: 60, y: 38 },
			{ x: 70, y: 38 },
			{ x: 80, y: 34 },
			{ x: 90, y: 44 }
		]
		},
		{
		showInLegend: true,
		type: "line",
		dataPoints: [
			{ x: 10, y: 25 },
			{ x: 20, y: 30 },
			{ x: 30, y: 20 },
			{ x: 40, y: 45 },
			{ x: 50, y: 30 },
			{ x: 60, y: 10 },
			{ x: 70, y: 15 },
			{ x: 80, y: 18 },
			{ x: 90, y: 20 }
		]
		}
	]
});

chart.render();

document.getElementById("showAllSeries").onclick =  function(){
	chart.options.data.forEach(function(dataSeries) {
  	if (typeof (dataSeries.visible) === "undefined" || dataSeries.visible) {
			dataSeries.visible = false;
		} else {
			dataSeries.visible = true;
		}
	});
	/*var dataSeries;
  	for(var i = 0; i < chart.data.length; i++){
  	dataSeries = chart.options.data[i];
		if (typeof (dataSeries.visible) === "undefined" || dataSeries.visible) {
			dataSeries.visible = false;
		} else {
			dataSeries.visible = true;
		}
	}*/  
	chart.render();
};
代码语言:javascript
复制
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<div id="chartContainer" style="height: 260px; width: 100%;"></div>
<center><button id= "showAllSeries" style= "margin: 10px;">Show/Hide All series</button></center>

票数 2
EN

Stack Overflow用户

发布于 2018-06-06 17:07:02

感谢Vishwas提供了详细的答案。一般来说-是的,for和forEach在这里都可以很好地使用。我会将它标记为正确的,但它帮助我使用forEach获得了我所期望的更简洁的解决方案:

代码语言:javascript
复制
document.getElementById(""showAllSeries"").onclick =  function(){
 chart.options.data.forEach(function(dataSeries) {
    dataSeries.visible = !dataSeries.visible })
    chart.render();
    };

我也会在这里留下一个历史:

代码语言:javascript
复制
 var chart = new CanvasJS.Chart("chartContainer",
    {
      title:{
      text: "Test Button to Hide All Series"  
      },
      
      legend: {
            cursor: "pointer",
            itemclick: function (e) {
                //console.log("legend click: " + e.dataPointIndex);
                //console.log(e);
                if (typeof (e.dataSeries.visible) === "undefined" || e.dataSeries.visible) {
                    e.dataSeries.visible = false;
                } else {
                    e.dataSeries.visible = true;
                }
 
                e.chart.render();
            }
        },
      
      data: [
      { 
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 5 },
        { x: 20, y: 9},
        { x: 30, y: 17 },
        { x: 40, y: 32 },
        { x: 50, y: 22 },
        { x: 60, y: 14 },
        { x: 70, y: 25 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 31 },
        { x: 20, y: 35},
        { x: 30, y: 30 },
        { x: 40, y: 35 },
        { x: 50, y: 35 },
        { x: 60, y: 38 },
        { x: 70, y: 38 },
        { x: 80, y: 34 },
        { x: 90, y: 44}
      
        ]
      },
        {
        showInLegend: true,
        type: "line",
        dataPoints: [
        { x: 10, y: 25 },
        { x: 20, y: 30},
        { x: 30, y: 20 },
        { x: 40, y: 45 },
        { x: 50, y: 30 },
        { x: 60, y: 10 },
        { x: 70, y: 15 },
        { x: 80, y: 18 },
        { x: 90, y: 20}
      
        ]
      }
      ]
    });
 
    chart.render();
    
    document.getElementById("showAllSeries").onclick=function(){
      chart.options.data.forEach(function(dataSeries){
		dataSeries.visible = !dataSeries.visible
            })
     chart.render();
     };
代码语言:javascript
复制
<script src="https://canvasjs.com/assets/script/canvasjs.min.js"></script>
<br/>
<div id="chartContainer" style="height: 300px; width: 100%;"></div>
<center><button id= "showAllSeries" style= "margin: 10px;">Show/Hide All series</button></center>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50708134

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档