Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

javascript - Add Custom Labels to Axes in Highcharts

I have a Highchart in my application showing car sales in each month. Given a user input value for a date, chart should show car sales in last 12 months. If user filter date is 2018-09-10, it should show car sales of Sep-2017, Oct-2017, Nov-2017, Dec-2017, Jan-2018, Feb-2018, Mar-2018, Apr-2018, May-2018, Jun-2018, Jul-2018, Aug-2018, Sep-2018, Oct-2018, Nov-2018, Dec-2018.

And also I need to show a year separation in the chart itself. I found an image similar to it, Year Separation between 2014 and 2015. I need something like it in my chart also. Is there any way that I can do it?

The following fiddle contains a sample code. I need to show the year separation of 2017 and 2018.

// Create the chart
Highcharts.chart('container', {
    chart: {
        type: 'column'
    },
    title: {
        text: 'Car Sales'
    },
    xAxis: {
        type: 'category'
    },
    yAxis: {
        title: {
            text: 'Total percent Car Sales'
        }

    },
    legend: {
        enabled: false
    },
    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true,
                format: '{point.y:.1f}%'
            }
        }
    },

    tooltip: {
        headerFormat: '<span style="font-size:11px">{series.name}</span><br>',
        pointFormat: '<span style="color:{point.color}">{point.name}</span>: <b>{point.y:.2f}%</b> of total<br/>'
    },

    "series": [
        {
            "name": "Car Sales",
            "colorByPoint": true,
            "data": [
                {
                    "name": "July",
                    "y": 62.74
                },{
                    "name": "August",
                    "y": 62.74
                },{
                    "name": "September",
                    "y": 62.74
                },{
                    "name": "October",
                    "y": 62.74
                },{
                    "name": "November",
                    "y": 62.74
                },{
                    "name": "December",
                    "y": 62.74
                },{
                    "name": "January",
                    "y": 62.74
                },
                {
                    "name": "February",
                    "y": 10.57
                },
                {
                    "name": "March",
                    "y": 7.23
                },
                {
                    "name": "April",
                    "y": 5.58
                },
                {
                    "name": "May",
                    "y": 4.02
                },
                {
                    "name": "June",
                    "y": 1.92
                },
                {
                    "name": "July ",
                    "y": 7.62
                }
            ]
        }
    ]
});

https://jsfiddle.net/yasirunilan/8xvjcd5h/1/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use Highcharts.SVGRenderer to create separator line and text with year. If you always have categories on the xAxis, you can do it in this way:

function renderSeparator(xVal, chart, year) {
  var xAxis = chart.xAxis[0],
    pxVal = xAxis.toPixels(xVal + 0.5);

  chart.renderer.path([
    'M', pxVal, chart.plotTop,
    'L', pxVal, chart.plotTop + chart.plotHeight
  ]).attr({
    stroke: 'black',
    'stroke-width': 1
  }).add();

  chart.renderer.text(
    year, pxVal + 10, 70
  ).css({
    color: 'black',
    fontSize: 20
  }).attr({
    zIndex: 6
  }).add();
}

// Create the chart
Highcharts.chart('container', {
  chart: {
    events: {
      render: function() {
        var chart = this,
          xAxis = chart.xAxis[0],
          year = 2014;

        chart.renderer.text(year, chart.plotLeft + 10, 70)
          .css({
            color: 'black',
            fontSize: 20
          })
          .attr({
            zIndex: 6
          })
          .add();

        year++;

        Highcharts.each(xAxis.names, function(el, i) {
          if (el === "December") {
            renderSeparator(i, chart, year);
            year++;
          }
        });
      }
    },
    type: 'column'
  },

  ...

});

Live demo: https://jsfiddle.net/BlackLabel/0ma6efcz/

API Reference: https://api.highcharts.com/class-reference/Highcharts.SVGRenderer#text


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...