简体   繁体   中英

Highcharts: Change scale sizes

Let´s say you have an x-axis that goes [0, 3, 6, ...] and a y-axis that is like [0, 5, 10, ...].

Highcharts handles those values so that automatically , somehow a difference of 5 in y direction does not look bigger than a difference of 3 in x direction.

How can you change the distances between the values / make a 5 on the y axis appear as big as 5/3 of the change on the x axis? (so that pe a line from (0,0) to point (5,5) has a 45° angle)

Code example:

$.getJSON('https://cdn.jsdelivr.net/gh/highcharts/highcharts@v7.0.0/samples/data/usdeur.json', function (data) {
  Highcharts.chart('container', {
      chart: {
        zoomType: 'x'
      },
      title: {
        text: 'USD to EUR exchange rate over time'
      },
      subtitle: {
        text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in'
      },
      xAxis: {
        type: 'datetime'
      },
      yAxis: {
        title: {
          text: 'Exchange rate'
        }
      },
      legend: {
        enabled: false
      },
      plotOptions: {
        area: {
          fillColor: {
            linearGradient: {
                x1: 0,
                y1: 0,
                x2: 0,
                y2: 1
            },
            stops: [
                [0, Highcharts.getOptions().colors[0]],
                [1, Highcharts.Color(Highcharts.getOptions().colors[0]).setOpacity(0).get('rgba')]
            ]
          },
          marker: {
            radius: 2
          },
          lineWidth: 1,
          states: {
            hover: {
              lineWidth: 1
            }
          },
          threshold: null
        }
      },
      series: [{
        type: 'area',
        name: 'USD to EUR',
        data: data
      }]
  });
});

taken from demo

In the load event, you can calculate and adjust the height or width of the chart:

chart: {
    events: {
        load: function() {
            var xAxis = this.xAxis[0],
                yAxis = this.yAxis[0];

            // Adjust xAxis
            this.setSize(
                yAxis.height / (yAxis.max - yAxis.min) *
              (xAxis.max - xAxis.min) + this.plotLeft + this.chartWidth -
              (this.plotLeft + this.plotWidth),
              null,
              false
            );
        }
    }
},

Live demo: http://jsfiddle.net/BlackLabel/64Lxutce/

or if you do not want to change the size, you can adjust one of the axis extremes:

chart: {
    events: {
        load: function() {
            var xAxis = this.xAxis[0],
                yAxis = this.yAxis[0],
                xAxisMax = xAxis.width /
                        (yAxis.height / (yAxis.max - yAxis.min)),
                yAxisMax = yAxis.height /
                        (xAxis.width / (xAxis.max - xAxis.min));

            if (xAxisMax < xAxis.max) {
                this.update({
                    yAxis: {
                        max: yAxisMax - yAxis.min
                    }
                }, true, true, false);
            } else {
                this.update({
                    xAxis: {
                        max: xAxisMax - xAxis.min
                    }
                }, true, true, false);
            }
        }
    }
},

Live demo: http://jsfiddle.net/BlackLabel/w3byrL28/

API Reference:

https://api.highcharts.com/highcharts/chart.events.load

https://api.highcharts.com/class-reference/Highcharts.Chart#update

https://api.highcharts.com/class-reference/Highcharts.Chart#setSize

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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