简体   繁体   English

如何在Highcharts的图例区域重置项目?

[英]How to reset items in legend area in Highcharts?

I have met a weird requirement for Highcharts : 我满足了Highcharts的怪异要求:

In Highcharts we can choose to show the legend or not,the items value is coming from the data.But now I need to remove the duplicate item if they have the same color and reset the value to a new one. 在Highcharts中,我们可以选择显示还是不显示图例,这些项的值来自数据。但是现在,如果它们具有相同的颜色,则需要删除重复的项并将其重置为新值。

As the image shows below: Task C and Task E have the same color,so we only need to preserve one item and rename it. 如下图所示: 任务CTask E具有相同的颜色,因此我们只需要保留一项并将其重命名即可。

I have checked on the official website at highcharts ,but still do not find an effective way,I have attached my test code below,can anyone help me,please? 我已经在highcharts的官方网站上进行了检查 ,但是仍然找不到有效的方法,我在下面附加了我的测试代码,请问有人可以帮助我吗?

Thanks in advance! 提前致谢!

Description image: 说明图片:

在此处输入图片说明

Test code: 测试代码:

  function formatDate(val){ var d = new Date(val); var cd = d.getDate(); var cm = d.getMonth() + 1; var cy = d.getFullYear(); return cy + "-" + cm + "-" + cd; } $(function () { $('#container').highcharts({ chart: { type: 'columnrange', inverted: true }, title: { text: 'Equipments Usage' }, subtitle: { text: 'Display based on date' }, xAxis: { categories: ['Equipment A', 'Equipment B', 'Equipment C', 'Equipment D', 'Equipment E', 'Equipment F'], gridLineDashStyle: 'longdash', gridLineWidth: 1, }, yAxis: { title: { text: 'Equipment use time range' }, type:'datetime', labels: { formatter:function(){ return formatDate(this.value); } }, }, plotOptions: { columnrange: { dataLabels: { enabled: true, formatter: function () { if(this.y == this.point.low){ return ""; } if(this.series.options.finished){ return "<span style='color:green'>\●</span> "; } } } } }, tooltip: { formatter:function(){ return this.series.name + '<br/>' + 'Start: <b>' + formatDate(this.point.low) + '</b><br/>' + 'End: <b>' + formatDate(this.point.high) + '</b><br/>' + 'Percent: <b>' + this.series.options.percent + '</b>'; } }, legend: { enabled: true, borderWidth:1, labelFormatter:function(){ return this.name; } }, series: [ { name: 'Task A', percent:'20%', finished:true, color:'lightgray', data: [ [1, Date.UTC(2018, 2, 6, 10), Date.UTC(2018, 2, 16, 10)], [3, Date.UTC(2018, 2, 9, 10), Date.UTC(2018, 2, 26, 10)], ], }, { name: 'TaskB', percent:'40%', finished:false, color:'lightblue', data: [ [1, Date.UTC(2018, 3, 6, 10), Date.UTC(2018, 4, 16, 10)], [2, Date.UTC(2018, 4, 26, 10), Date.UTC(2018, 5, 10, 10)], [3, Date.UTC(2018, 3, 8, 10), Date.UTC(2018, 4, 5, 10)] ], }, { name: 'Task C', percent:'60%', finished:false, color:'#b2f442', data: [ [0, Date.UTC(2018, 3, 26, 10), Date.UTC(2018, 4, 6, 10)], [1, Date.UTC(2018, 3, 16, 10), Date.UTC(2018, 3, 26, 10)], [4, Date.UTC(2018, 4, 10), Date.UTC(2018, 5, 6, 10)], ], }, { name: 'Task D', percent:'60%', finished:false, color:'red', data: [ [0, Date.UTC(2018, 5, 26, 10), Date.UTC(2018, 6, 6, 10)], [1, Date.UTC(2018, 5, 26, 10), Date.UTC(2018, 6, 16, 10)] ], }, { name: 'Task E', percent:'55%', finished:false, color:'#b2ff42', data: [ [2, Date.UTC(2018, 6, 1, 10), Date.UTC(2018, 6, 16, 10)], [4, Date.UTC(2018, 6, 1, 10), Date.UTC(2018, 6, 10, 10)] ], } ] }); }); 
  <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/highcharts-more.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

Test code run result 测试代码运行结果
在此处输入图片说明

check plotOptions.series.linkedTo and change the series name accordingly 检查plotOptions.series.linkedTo并相应地更改系列名称

linkedTo: string The id of another series to link to. linkedTo:字符串要链接到的另一个系列的ID。 Additionally, the value can be ":previous" to link to the previous series. 此外,该值可以是“:previous”以链接到上一个系列。 When two series are linked, only the first one appears in the legend. 当两个系列链接在一​​起时,只有第一个出现在图例中。 Toggling the visibility of this also toggles the linked series. 切换可见性也会切换链接的系列。 Defaults to undefined. 默认为未定义。

 function formatDate(val) { var d = new Date(val); var cd = d.getDate(); var cm = d.getMonth() + 1; var cy = d.getFullYear(); return cy + "-" + cm + "-" + cd; } $(function() { $('#container').highcharts({ chart: { type: 'columnrange', inverted: true }, title: { text: 'Equipments Usage' }, subtitle: { text: 'Display based on date' }, xAxis: { categories: ['Equipment A', 'Equipment B', 'Equipment C', 'Equipment D', 'Equipment E', 'Equipment F'], gridLineDashStyle: 'longdash', gridLineWidth: 1, }, yAxis: { title: { text: 'Equipment use time range' }, type: 'datetime', labels: { formatter: function() { return formatDate(this.value); } }, }, plotOptions: { columnrange: { dataLabels: { enabled: true, formatter: function() { if (this.y == this.point.low) { return ""; } if (this.series.options.finished) { return "<span style='color:green'>\●</span> "; } } } } }, tooltip: { formatter: function() { return this.series.name + '<br/>' + 'Start: <b>' + formatDate(this.point.low) + '</b><br/>' + 'End: <b>' + formatDate(this.point.high) + '</b><br/>' + 'Percent: <b>' + this.series.options.percent + '</b>'; } }, legend: { enabled: true, borderWidth: 1, labelFormatter: function() { return this.name; } }, series: [{ name: 'Task A', percent: '20%', finished: true, color: 'lightgray', data: [ [1, Date.UTC(2018, 2, 6, 10), Date.UTC(2018, 2, 16, 10)], [3, Date.UTC(2018, 2, 9, 10), Date.UTC(2018, 2, 26, 10)], ], }, { name: 'TaskB', percent: '40%', finished: false, color: 'lightblue', data: [ [1, Date.UTC(2018, 3, 6, 10), Date.UTC(2018, 4, 16, 10)], [2, Date.UTC(2018, 4, 26, 10), Date.UTC(2018, 5, 10, 10)], [3, Date.UTC(2018, 3, 8, 10), Date.UTC(2018, 4, 5, 10)] ], }, { name: 'Task C', id: 'taskc', percent: '60%', finished: false, color: '#b2f442', data: [ [0, Date.UTC(2018, 3, 26, 10), Date.UTC(2018, 4, 6, 10)], [1, Date.UTC(2018, 3, 16, 10), Date.UTC(2018, 3, 26, 10)], [4, Date.UTC(2018, 4, 10), Date.UTC(2018, 5, 6, 10)], ], }, { name: 'Task D', percent: '60%', finished: false, color: 'red', data: [ [0, Date.UTC(2018, 5, 26, 10), Date.UTC(2018, 6, 6, 10)], [1, Date.UTC(2018, 5, 26, 10), Date.UTC(2018, 6, 16, 10)] ], }, { name: 'Task E', percent: '55%', finished: false, linkedTo: 'taskc', color: '#b2ff42', data: [ [2, Date.UTC(2018, 6, 1, 10), Date.UTC(2018, 6, 16, 10)], [4, Date.UTC(2018, 6, 1, 10), Date.UTC(2018, 6, 10, 10)] ], } ] }); }); 
 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/highcharts-more.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> 

In order to achieve expected effect, you need to do an iteration over all series on chart.events.load , and check if some pair of series has the same color. 为了获得预期的效果,您需要对chart.events.load上的所有系列进行chart.events.load ,并检查某对系列是否具有相同的颜色。 If yes, then just update the name of first series, and set second series linkedTo parameter equal to name you set on first one. 如果是,则只需更新第一个系列的名称,然后将第二个系列的linkedTo参数设置为等于您在第一个系列上设置的名称。 Please take a look at code below: 请看下面的代码:

   chart: {
     type: 'columnrange',
     inverted: true,
     events: {
       load() {
         var chart = this,
           series = chart.series,
           mergedCount = 1;
         series.forEach(series1 => {
           series.forEach(series2 => {
             if (
               series1.options.color === series2.options.color &&
               series1 !== series2 &&
               !series1.options.linkedTo &&
               !series2.options.linkedTo
             ) {
               series1.update({
                 name: 'Merged series ' + mergedCount
               })
               series2.update({
                 linkedTo: 'Merged series ' + mergedCount
               })
             }
           })
         })
       }
     }
   }

Now, you've only make sure that series you want to 'merge' has the same color. 现在,您只需确保要“合并”的系列具有相同的颜色。

Live example: https://jsfiddle.net/o83j7bkc/ 实时示例: https //jsfiddle.net/o83j7bkc/

API Reference: API参考:

https://api.highcharts.com/highcharts/chart.events.load https://api.highcharts.com/highcharts/chart.events.load

https://api.highcharts.com/class-reference/Highcharts.Series#update https://api.highcharts.com/class-reference/Highcharts.Series#update

https://api.highcharts.com/highcharts/plotOptions.series.linkedTo https://api.highcharts.com/highcharts/plotOptions.series.linkedTo

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

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