javascript - Highcharts:更改比例大小

标签 javascript highcharts scale

假设您有一个 x 轴,方向为 [0, 3, 6, ...] 和一个 y 轴,类似于 [0 , 5, 10, ...]。

Highcharts 会自动处理这些值,以某种方式,y 方向上 5 的差异看起来不会比 x 方向上 3 的差异更大

如何更改值之间的距离/使 y 轴上的 5 显示为 x 轴上变化的 5/3? (因此,从 (0,0) 到点 (5,5) 的线有 45° Angular )

代码示例:

$.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
      }]
  });
});

取自demo

最佳答案

load事件中,您可以计算和调整图表的高度或宽度:

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
            );
        }
    }
},

现场演示: http://jsfiddle.net/BlackLabel/64Lxutce/

或者,如果您不想更改大小,可以调整轴极值之一:

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);
            }
        }
    }
},

现场演示: http://jsfiddle.net/BlackLabel/w3byrL28/

API引用:

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

关于javascript - Highcharts:更改比例大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55142315/

相关文章:

php - Yii 和 ActiveRecord 按计数分组

frameworks - 说一个框架 “scales well”是什么意思?

scale - PDFsharp - 更改页面大小和缩放内容

Android:如何以原始大小显示图像

javascript - 如何定义要为 Django 表单/小部件/管理员呈现的内联 Javascript?

javascript - 如何使用 Artillery 自动生成 OAuth 2.0 token ?

javascript - Highcharts 3D 散点不在工具提示中显示名称

javascript - HighCharts 线系列无法与堆叠条形组合图正确显示

javascript - 将对象属性中的数组合并到对象数组中

javascript - javascript中的方法链