隐藏所有数据集时如何不显示比例尺(chartjs)?

时间:2019-05-09 08:22:12

标签: django chart.js

我正在使用django和chartjs渲染图表。当我使用图例onclick函数隐藏所有数据集时,y轴将像这样显示,而我不希望显示任何内容,甚至不显示网格线。

这是示例图表:

enter image description here

有什么解决方案或建议吗?

2 个答案:

答案 0 :(得分:1)

I put together an example在不完全知道要显示/隐藏的内容的情况下,在所有系列均关闭时隐藏了x / y轴。为此,我按照其文档中有关how write an onClick handler for the legend的说明进行操作。

以下是该处理程序的相关代码:

{
  ...
  options: {
    scales: {
        yAxes: [{
        display: true, // <-- Added to make sure property was in options to read
        ...
      }],
      xAxes: [{
        display: true // <-- Added to make sure property was in options to read
      }]
    },
    legend: {
      onClick: function (e, legendItem) {
        const index = legendItem.datasetIndex;
        const ci = this.chart;
        var meta = ci.getDatasetMeta(index);

        meta.hidden = meta.hidden === null ? !ci.data.datasets[index].hidden : null;

        /* Start: Custom code to handle the showing/hiding */
        const allHidden = ci.data.datasets.every((dataSet, index) => ci.getDatasetMeta(index).hidden);

        ci.options.scales.yAxes[0].display = !allHidden;
        ci.options.scales.xAxes[0].display = !allHidden;
        /* End: Custom code to handle the showing/hiding */

        // We hid a dataset ... rerender the chart
        ci.update();
      }
    }
  }
});

答案 1 :(得分:1)

我遵循Daniel的建议,并重写了自定义onClick函数。现在它的工作完全符合我的预期。

 private void MWebview_Download(object sender, DownloadEventArgs e)
 {
   var url = e.Url;
   DownloadManager.Request request = new 
  DownloadManager.Request(Uri.Parse(url));

request.AllowScanningByMediaScanner();
request.SetNotificationVisibility(DownloadManager.Request.VisibilityVisibleNotifyCompleted); //Notify client once download is completed!
request.SetDestinationInExternalPublicDir(Environment.DirectoryDownloads, "CPPPrimer");
DownloadManager dm = (DownloadManager)GetSystemService("download");
dm.Enqueue(request);
Toast.MakeText(ApplicationContext, "Downloading File",ToastLength.Long//To notify the Client that the file is being downloaded
            ).Show();
}