Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make highcharts scrollable horizontally when having big range in x-axis

Tags:

highcharts

I am generating my x-axis and y-axis data dynamically and displaying highcharts, but the chart becomes messy when the x-axis range is high with small intervals.

How do I make highcharts to make normal horizontally scrollable graph?

Here is what I am using right now:

enter image description here

<div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>

//CODE FOR HIGHCHARTS JS
function makeChart() {
    $('#container').highcharts({
        chart: {
            type: 'line',
            marginRight: 130,
            marginBottom: 100
        },
        title: {
            text: 'Banana',
            x: -20 //center
        },
        subtitle: {
            text: 'Source: banana.com',
            x: -20
        },
        xAxis: {
            categories: xlist
        },
        yAxis: {
            title: {
                text: 'No. of C'
            },
            plotLines: [{
                value: 0,
                width: 1,
                color: '#808080'
            }]
        },
        tooltip: {
            valueSuffix: 'C'
        },
        legend: {
            layout: 'vertical',
            align: 'right',
            verticalAlign: 'top',
            x: -10,
            y: 100,
            borderWidth: 0
        },
        series: [{
            name: $("#repoSelector option:selected").text(),
            data: ylist
        }]
    });
}
like image 320
scottydelta Avatar asked May 23 '13 05:05

scottydelta


People also ask

How do you handle horizontal scrollbar?

To enable horizontal scrolling, we can use the CSS property overflow-x. If we assign the value scroll to the overflow-x property of the container element, the browser will hide horizontally overflowing content and make it accessible via horizontal scrolling.

Is Highcharts responsive?

Since Highcharts 5.0 you can create responsive charts much the same way you work with responsive web pages. A top-level option, responsive, exists in the configuration. One of the most handy options is chart.

How do I change the y axis value in Highcharts?

A simpler way to go about this is using the tickInterval attribute: yAxis: { title: { text: 'Percent' }, tickInterval: 10, labels: { formatter: function(){ return this. value + '%'; } } }, That will force your axis intervals to show up as you requested.


3 Answers

Two ways to achieve a scroll bar.

Option 1

You will need to use highstock.js and instead of rendering a stock chart, you have to render a highchart.

Then enable the scroll bar

  scrollbar: {
        enabled: true
    }

Check the API for scroll bar and related operations here.

Here I have fiddled an example.

Option 2

Try setting min & max attributes to the x-axis.

xAxis: {
            categories: [...],
            min: 0,
            max:9
}

Displays 10 categories in x-axis at a stretch, adding a scroll for the rest of the categories. find the fiddled example here.

like image 151
Gopinagh.R Avatar answered Oct 11 '22 23:10

Gopinagh.R


add this

const Highcharts = require('highcharts/highstock');

comment this if you have

// import Highcharts from 'highcharts';

change xAxis to like this

xAxis : {
               categories: [],
                min:0,
                max:9,
                scrollbar: {
                    enabled: true
                }
            }
like image 41
Krishantha Dinesh Avatar answered Oct 12 '22 00:10

Krishantha Dinesh


To enable scrollbar in x-axis, try this

xAxis: {
  categories: xlist,
  min: 0,
  max: 4,
  scrollbar: {
        enabled: true
  },
  },

Check the jfiddle here: https://jsfiddle.net/BhavyaAprajita/zja90wf2/1/

Also, make sure that you import the highstock library

src="https://code.highcharts.com/stock/highstock.js"
like image 43
Bhavya Aprajita Avatar answered Oct 12 '22 00:10

Bhavya Aprajita