Search code examples
javascriptreactjshighchartshighcharts-ngreact-highcharts

Want to set the y axis ticks to a fixed value Highcharts


I am trying a combination of line and an area graph based on data points based.

In Graph 1, Y axis looks clumsy with lot of ticks. Is there a way where these ticks can be set to may be "3" on y axis. This should remain constant for any data points, ticks should always 3

Also in the graph 2, there is an extra horizontal line, below 0. Is there any way I could avoid it, so that reference is always from 0 than having lines below 0.

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

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
    },
    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 +
            " (" +
            elem.point.ts +
            ")</b></td></tr>";
        });
        return formattedString;
      }
    },
    colors: props.legendColor,
    xAxis: {
      visible: true,
      step: 1,
      tickInterval: 6,
      categories: props.categories
    },
    yAxis: {
      visible: true,
      step: 1,
      tickInterval: 1,
      title: {
        enabled: true,
        text: "Value",
        style: {
          fontWeight: "100"
        }
      }
    },
    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;


Solution

  • In Graph 1, Y axis looks clumsy with lot of ticks. Is there a way where these ticks can be set to may be "3" on y axis. This should remain constant for any data points, ticks should always 3

    Use tickAmount property:

    yAxis: {
      tickAmount: 3,
      ..
    }
    

    API Reference: https://api.highcharts.com/highcharts/yAxis.tickAmount


    Also in the graph 2, there is an extra horizontal line, below 0. Is there any way I could avoid it, so that reference is always from 0 than having lines below 0.

    Yes, you can for example set softMax property. Highcharts is unable to calculate an axis scale if there are only 0 values.

    yAxis: {
      softMax: 1,
      ...
    }
    

    API Reference: https://api.highcharts.com/highcharts/yAxis.softMax