Search code examples
javascriptreactjshighchartshighcharts-ngreact-highcharts

Combining graphs in highcharts


I am trying to plot two line graphs based on the dynamic data from the server. These graphs are comparison based graphs, where in data points are compared over time

I am trying to make one of the graph as a area graph and the other one remains line graph. In my case Current should be a line graph where as Baseline should be an area graph

Sandbox: https://codesandbox.io/s/react-line-chart-n9g6o?file=/src/LineChart.js

Here is what I have tried

import * as React from "react";
import Highcharts from "highcharts";
import HighchartsReact from "highcharts-react-official";
import HC_exporting from "highcharts/modules/exporting";
HC_exporting(Highcharts);

function LineChart(props) {
  let chartOptions = {
    chart: {
      type: "line",
      height: props.height
    },
    credits: false,
    exporting: { enabled: false },
    title: {
      text: ""
    },
    legend: {
      align: "center",
      verticalAlign: "bottom",
      x: 0,
      y: 0
    },
    tooltip: {
      shared: true,
      useHTML: true,
      formatter: function() {
        let self = this;
        let formattedString = "<small></small><table>";
        self.points.forEach(elem => {
          formattedString +=
            '<tr><td style="color: {series.color}">' +
            elem.series.name +
            ": </td>";
          formattedString +=
            '<td style="text-align: right"><b>' + elem.y + "</b></td></tr>";
        });
        return formattedString;
      }
    },
    colors: props.legendColor,
    xAxis: {
      visible: false
    },
    yAxis: {
      visible: true,
      step: 1
    },
    plotOptions: {
      column: {
        dataLabels: {
          enabled: true,
          crop: false,
          overflow: "none"
        }
      },
      line: {
        marker: {
          enabled: false
        }
      }
    },
    series: props.chartData
  };
  return <HighchartsReact highcharts={Highcharts} options={chartOptions} />;
}

export default LineChart;

Can some one help me here? Help would be appreciated.


Solution

  • You only need to set the proper type in a series configuration object:

    data.push({
      name: "BASELINE",
      type: 'area',
      data: old,
      keys: ["x", "y", "ots"]
    });
    

    Live demo: https://codesandbox.io/s/react-line-chart-2ohem?file=/src/index.js

    API Reference: https://api.highcharts.com/highcharts/series.area.type