Javascript Chart.js-加载后使用标签字符串隐藏线

Javascript Chart.js-加载后使用标签字符串隐藏线,javascript,chart.js,Javascript,Chart.js,使用,我有一个折线图,显示在.net web应用程序中编译的数据集 加载数据后,是否可以根据标签名称将行设置为隐藏/禁用 在chart.js中有一个允许在加载时隐藏数据集的选项,但这是在最初定义数据集时设置的。由于数据已经在视图中定义,我不能使用此标志。例如 var viewsByMonthChartCtx = new Chart(viewsByMonthChartCtx, { type: 'line', data: { labels: [

使用,我有一个折线图,显示在.net web应用程序中编译的数据集

加载数据后,是否可以根据标签名称将行设置为隐藏/禁用

在chart.js中有一个允许在加载时隐藏数据集的选项,但这是在最初定义数据集时设置的。由于数据已经在视图中定义,我不能使用此标志。例如

var viewsByMonthChartCtx = new Chart(viewsByMonthChartCtx, {
        type: 'line',
        data: {
            labels: ['February 2017','March 2017'],
            datasets: 
                 [ { label: 'Home', data: [100,120 ] }, // I want to set 'hidden: true' to the Home dataset post initialization
                   { label: 'Search', data: [200,240 ] } ]
        },
        options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero: true
                    }
                }]
            },
        }
    });

如果要在图表初始化(并呈现)后隐藏数据集,则仍然使用数据集
hidden:true
属性。您只需从图表实例访问数据集并将属性设置为true

下面是一个示例,其中与标签匹配的数据集在页面加载5秒后隐藏

// the dataset label we want to hide after chart is initialized
var hiddenLabel = 'My Second Dataset';
var timerDuration = 5;

// hide a dataset after X seconds
setTimeout(function() {
  var indexToHide = -1;  

  // find which dataset matches the label we want to hide
  myLine.config.data.datasets.forEach(function(e, i) {
    if (e.label === hiddenLabel) {
      indexToHide = i;
    }
  });

  // get the dataset meta object so we can hide it
  var meta = myLine.getDatasetMeta(indexToHide);

  // hide the dataset and re-render the chart
  meta.hidden = true;
  myLine.update();
}, timerDuration * 1000);
如您所见,您只需在数据集上迭代以找到具有匹配标签的数据集的索引,然后只需将hidden属性设置为true并更新图表

下面是一个演示完整工作示例的示例

话虽如此,但不清楚为什么在隐藏图表后要这样做。如果您已经知道要在页面加载时隐藏哪个数据集,那么您可以动态地组装数据和选项图表配置,并以编程方式将隐藏标志设置为true。这里有一个例子

// the dataset label we want to hide
var hiddenLabel = 'My Second Dataset';

// build our data and options config (if needed you could build the datasets dynamically from dynamic data (this example is static)
var config = {
  type: 'line',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First Dataset",
      backgroundColor: chartColors.red,
      borderColor: chartColors.red,
      data: [5, 10, 25, 15, 10, 20, 30],
      fill: false,
    }, {
      label: "My Second Dataset",
      fill: false,
      backgroundColor: chartColors.blue,
      borderColor: chartColors.blue,
      data: [5, 0, 12, 5, 25, 35, 15],
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'Chart.js Hide Dataset Matching "My Seconds Dataset" After 3 Seconds'
    },
    tooltips: {
      mode: 'index',
      intersect: false,
    },
    hover: {
      mode: 'nearest',
      intersect: true
    },
    scales: {
      xAxes: [{
        display: true,
        scaleLabel: {
          display: true,
          labelString: 'Month'
        }
      }],
    }
  }
};

// iterate over our datasets to find the one we want to hide
config.data.datasets.forEach(function(e) {
  if (e.label === hiddenLabel) {
    e.hidden = true;
  }
});

// instantiate the chart
var myLine = new Chart($('#canvas'), config);

如果要在图表初始化(并呈现)后隐藏数据集,则仍然使用数据集
hidden:true
属性。您只需从图表实例访问数据集并将属性设置为true

下面是一个示例,其中与标签匹配的数据集在页面加载5秒后隐藏

// the dataset label we want to hide after chart is initialized
var hiddenLabel = 'My Second Dataset';
var timerDuration = 5;

// hide a dataset after X seconds
setTimeout(function() {
  var indexToHide = -1;  

  // find which dataset matches the label we want to hide
  myLine.config.data.datasets.forEach(function(e, i) {
    if (e.label === hiddenLabel) {
      indexToHide = i;
    }
  });

  // get the dataset meta object so we can hide it
  var meta = myLine.getDatasetMeta(indexToHide);

  // hide the dataset and re-render the chart
  meta.hidden = true;
  myLine.update();
}, timerDuration * 1000);
如您所见,您只需在数据集上迭代以找到具有匹配标签的数据集的索引,然后只需将hidden属性设置为true并更新图表

下面是一个演示完整工作示例的示例

话虽如此,但不清楚为什么在隐藏图表后要这样做。如果您已经知道要在页面加载时隐藏哪个数据集,那么您可以动态地组装数据和选项图表配置,并以编程方式将隐藏标志设置为true。这里有一个例子

// the dataset label we want to hide
var hiddenLabel = 'My Second Dataset';

// build our data and options config (if needed you could build the datasets dynamically from dynamic data (this example is static)
var config = {
  type: 'line',
  data: {
    labels: ["January", "February", "March", "April", "May", "June", "July"],
    datasets: [{
      label: "My First Dataset",
      backgroundColor: chartColors.red,
      borderColor: chartColors.red,
      data: [5, 10, 25, 15, 10, 20, 30],
      fill: false,
    }, {
      label: "My Second Dataset",
      fill: false,
      backgroundColor: chartColors.blue,
      borderColor: chartColors.blue,
      data: [5, 0, 12, 5, 25, 35, 15],
    }]
  },
  options: {
    responsive: true,
    title: {
      display: true,
      text: 'Chart.js Hide Dataset Matching "My Seconds Dataset" After 3 Seconds'
    },
    tooltips: {
      mode: 'index',
      intersect: false,
    },
    hover: {
      mode: 'nearest',
      intersect: true
    },
    scales: {
      xAxes: [{
        display: true,
        scaleLabel: {
          display: true,
          labelString: 'Month'
        }
      }],
    }
  }
};

// iterate over our datasets to find the one we want to hide
config.data.datasets.forEach(function(e) {
  if (e.label === hiddenLabel) {
    e.hidden = true;
  }
});

// instantiate the chart
var myLine = new Chart($('#canvas'), config);

这很有效-谢谢你。我无法在获取数据之前加载它的原因是,数据在.net中生成并输出到视图;我没有访问.net代码的权限,但我可以通过JS进行更新。但是你是对的,在加载视图之前隐藏它会容易得多!这很有效-谢谢你。我无法在获取数据之前加载它的原因是,数据在.net中生成并输出到视图;我没有访问.net代码的权限,但我可以通过JS进行更新。但是你是对的,在加载视图之前隐藏它会容易得多!