简体   繁体   English

使用highchart.js绘制图表

[英]Draw a chart using highchart.js

I am very new to data visulizations. 我对数据可视化非常陌生。 Currently we are having a requirement to draw a chart similar to below using HighChart.js 当前,我们需要使用HighChart.js绘制类似于以下的图表 在此处输入图片说明

See jsfiddle: http://jsfiddle.net/rgpz2ru5/22/ for what I tried so far. 请参阅jsfiddle: http : //jsfiddle.net/rgpz2ru5/22/了解我到目前为止的尝试。 I am successfully able to draw chart but facing issue while drawing lines between the datalabel and point (just shown in above image)? 我能够成功绘制图表,但在datalabel和point之间绘制线时遇到问题(如上图所示)? Can you please help? 你能帮忙吗?

See below code to draw a chart: 参见下面的代码绘制图表:

 $('#container').highcharts({
    chart: {
      type: 'scatter',
    },
    xAxis: {
      visible: false
    },
    yAxis: {
      title: {
        text: ''
      },
      labels: {
        formatter: function() {
          return null
        }
      }
    },
    plotOptions: {
      series: {
        dataLabels: {
          enabled: true,
          inside: false,
            formatter: function(){
            console.log("X"+this.x)
            console.log("this.x%2"+this.x%2)
          if(this.x%2 == 0){
          console.log("in if")
          return "<div style='position: relative;height:70px; border-left:solid thin #ff0000;margin-left:10px'><span style='position: absolute;bottom: 0;right: 0;'>hello</span></div>";
          }else{
          console.log("in else")
          return "<span style='color:black'>"+this.key+"</span><div style='height:70px; border-left:solid thin #ff0000;margin-left:10px'/>";
          }
          },
          useHTML:true

        }
      },scatter: {
                marker: {
                    radius: 5,
                    states: {
                        hover: {
                            enabled: false,
                            lineColor: "#ffb74d"
                        }
                    }
                },
                states: {
                    hover: {
                        marker: {
                            enabled: false
                        }
                    }
                },
                tooltip: {
                    headerFormat: '<b>{series.name}</b><br>',
                    pointFormat: '{point.x} cm, {point.y} kg'
                }
            }
    },

    series: [{
      data: [{
        x: 1,
        y: 1,
         dataLabels: {
          y: -80

        },
        marker: {
          radius: 3
        },
        name: 'SDS',
        color: "#ffb74d"
      }, {
        x: 2,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 5
        },
        name: 'MIP',
        color:"#ffe0b2"
      }, {
        x: 3,
        y: 1,
         dataLabels: {
          y: -80,
          distance : 50,
                    softConnector : false,
                    connectorColor : '#D0D0D0',
        },
        marker: {
          radius: 7
        },
        name: 'MDP',
        color:"#ff9800"
      },{
        x: 4,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 9
        },
        name: 'RAD',
        color:"#ffb74d"
      },{
        x: 5,
        y: 1,
         dataLabels: {
          y: -80
        },
        marker: {
          radius: 3
        },
        name: 'SDS',
        color: "#ffb74d"
      }, {
        x: 6,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 5
        },
        name: 'MIP',
        color:"#ffe0b2"
      }, {
        x: 7,
        y: 1,
         dataLabels: {
          y: -80
        },
        marker: {
          radius: 7
        },
        name: 'MDP',
        color:"#ff9800"
      },{
        x: 8,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 9
        },
        name: 'RAD',
        color:"#ffb74d"
      },{
        x: 9,
        y: 1,
         dataLabels: {
          y: -80
        },
        marker: {
          radius: 3
        },
        name: 'SDS',
        color: "#ffb74d"
      }, {
        x: 10,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 5
        },
        name: 'MIP',
        color:"#ffe0b2"
      }, {
        x: 11,
        y: 1,
         dataLabels: {
          y: -80
        },
        marker: {
          radius: 7
        },
        name: 'MDP',
        color:"#ff9800"
      },{
        x: 12,
        y: 1,
         dataLabels: {
          y: 80
        },
        marker: {
          radius: 9
        },
        name: 'RAD',
        color:"#ffb74d"
      }]
    }]

  }); 

Can you please help? 你能帮忙吗?

Your chart seems to be of a specific kind. 您的图表似乎属于特定种类。 I personally have not seen any such visualizations before. 我个人之前从未见过任何此类可视化。 Also, having worked with HighCharts, I doubt if any type of highchart can be transformed as yours. 另外,在与HighCharts合作之后,我怀疑是否可以将任何type的highchart转换为您自己的。

However, on the first look on your figure the following idea appeared in my mind with angular framework. 但是,在您的图形上初看时,以下想法在我的脑海中出现了。

You can achieve this kind of visualization using basic html and css. 您可以使用基本的html和css实现这种可视化。

Assuming you are aware with Angular terminology. 假设您了解Angular术语。 I suggest having a list of all your values. 我建议您列出所有价值观。 Loop them in a simple div as follows: 将它们循环到一个简单的div中,如下所示:

<div ng-repeat = "c in pointsList" > </div>

Next you can use ng-style or ng-class to add dynamic css to your each div. 接下来,您可以使用ng-styleng-class向每个div添加动态CSS。

Seeing your figure, I assume you only following css properties: 1. border-radius 2. background-color 3. width 4. height 看到您的图,我假设您仅遵循css属性:1. border-radius 2. background-color 3. width 4. height

Seems like you have fixed categories for each type of bubbles. 似乎您对每种气泡类型都有固定的类别。 So you can make few fixed classes in your css and apply them dynamically on the basis of your type of point. 因此,您可以在CSS中创建几个固定的类,并根据您的点的类型动态地应用它们。 Basically, it would look like: 基本上,它看起来像:

<div ng-repeat = "c in pointsList" ng-class = "{'css1' : c.type1, 'css2' : c.type2}" > </div>

Along with this you will need to apply display: inline-block to bring all divs in a line. 与此同时,您将需要应用display: inline-block将所有divs排成一行。

I know, its too naive solution, but if you have limited requirements of such chart, this can help you achieve it with minimal adjustments. 我知道,它的解决方案太幼稚,但是如果您对此类图表的要求有限,则可以通过最少的调整来帮助您实现。

To make similar chart you can use bubble chart: http://jsfiddle.net/rgpz2ru5/ 要制作类似的图表,您可以使用气泡图: http : //jsfiddle.net/rgpz2ru5/

Or standard scatter chart: http://jsfiddle.net/rgpz2ru5/1/ 或标准散点图: http : //jsfiddle.net/rgpz2ru5/1/

You can change marker size of scatter using marker.radius parameter: 您可以使用marker.radius参数更改散布的标记大小:

    marker: {
      radius: 5,
      states: {
        hover: {
          lineColor: "#ffb74d"
        }
      }
    },

You can add your labels using chart.renderer.path and chart.renderer.label: http://api.highcharts.com/highcharts/Renderer.path http://api.highcharts.com/highcharts/Renderer.label 您可以使用chart.renderer.path和chart.renderer.label添加标签: http : //api.highcharts.com/highcharts/Renderer.path http://api.highcharts.com/highcharts/Renderer.label

  var drawLabels = function(chart) {
    $('.cLabels').remove();
    var series = chart.series,
      renderer = chart.renderer,
      plotTop = chart.plotTop,
      plotLeft = chart.plotLeft;
    Highcharts.each(series, function(s) {
      Highcharts.each(s.data, function(p) {
        renderer.path(['M', p.plotX + plotLeft, p.plotY + plotTop, 'L', p.plotX + plotLeft, p.plotY + plotTop - 30]).attr({
          stroke: 'black',
          'stroke-width': 1
        }).addClass('cLabels').add();
        renderer.label(p.name, p.plotX + plotLeft, p.plotY + plotTop - 50).attr({
          'text-anchor': 'middle',
          padding: 0
        }).addClass('cLabels').add();
      });
    });
  }

Here you can see an example how it can work: http://jsfiddle.net/rgpz2ru5/29/ 在这里,您可以看到一个示例它如何工作的: http : //jsfiddle.net/rgpz2ru5/29/

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

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