简体   繁体   English

为什么在两个相似的图表中Highcharts的刻度线位置不同?

[英]Why is the Highcharts tick placement different in two similar charts?

I have a question regarding the tick placement in two similar Highcharts chart. 我对两个相似的Highcharts图表中的刻度线位置有疑问。 In the first example, I have specified the xAxis property and in the second example I have specified an x and y value in the series. 在第一个示例中,我指定了xAxis属性,在第二个示例中,我指定了系列中的xy值。 The data is exactly the same, but as you can see the ticks are placed differently. 数据完全相同,但是您可以看到刻度线的位置不同。 I have tried to play around with the tickmarkPlacement , minPadding , maxPadding to make the first example look like the second, but without success. 我尝试使用tickmarkPlacementminPaddingmaxPadding使第一个示例看起来像第二个示例,但是没有成功。

Could someone tell me wy the two charts, place ticks so different and how I could make the first example look like the second example? 有人可以告诉我两个图表,将刻度线如此不同,如何使第一个示例看起来像第二个示例吗?

The code and jsFiddles: 代码和jsFiddles:

First Example 第一个例子

The code: 编码:

$('#container').highcharts(
    {
        "series": [
            {
                "name": "Series 1",
                "data": [
                    69.78,
                    2235.83,
                    69.78,
                    908.36,
                    69.78,
                    174.78
                ],
                "color": "#0084cb",
                "opposite": false
            }
        ],
        "xAxis": {
            "categories": [
                "2015",
                "2016",
                "2017",
                "2018",
                "2019",
                "2020"
            ],
            "tickmarkPlacement": "on"
        }
    }
);

The chart: 图表:

在此处输入图片说明

http://jsfiddle.net/fw9o4fjh/5/ http://jsfiddle.net/fw9o4fjh/5/

Second Example 第二个例子

The code: 编码:

$('#container').highcharts(
    {
        "series": [
            {
                "name": "Series 1",
                "data": [
                    {
                        "y": 69.78,
                        "x": 2015
                    },
                    {
                        "y": 2235.83,
                        "x": 2016
                    },
                    {
                        "y": 69.78,
                        "x": 2017
                    },
                    {
                        "y": 908.36,
                        "x": 2018
                    },
                    {
                        "y": 69.78,
                        "x": 2019
                    },
                    {
                        "y": 174.78,
                        "x": 2020
                    }
                ],
                "color": "#0084cb",
                "opposite": false
            }
        ]
    }
);

The chart: 图表: 在此处输入图片说明

http://jsfiddle.net/y71f7hhd/4/ http://jsfiddle.net/y71f7hhd/4/

The difference is a categorical axis type vs a linear axis type. 区别在于分类轴类型与线性轴类型。

The categorical axis type reserves an even block of space for each category, which is why you see 'extra' space at the start and end of the x axis. 类别轴类型为每个类别保留一个偶数空间,这就是为什么您在x轴的开始和结束处看到“额外”空间的原因。

If you enable crosshairs on the chart, and hover over a point, you will see a good illustration of how it treats the axis spacing: 如果在图表上启用crosshairs ,并将鼠标悬停在一个点上,您将看到一个很好的说明,说明了它如何处理轴间距:

One of the things you can do to manipulate the categorical axis the way you want is to use the min and the max, like this: 您可以按照想要的方式操作分类轴的方法之一是使用最小值和最大值,如下所示:

xAxis: {
  min:0.4,
  max:4.6,
  ...

Example: 例:

It's far from exact, or ideal, but the categories work the way they do for logical reasons, and if you want your chart to plot like the linear example, the best thing to do is use a linear axis - or, since you're talking about dates, use a datetime axis type. 它远非精确或理想,但由于逻辑原因,类别按它们的方式工作,如果您希望图表像线性示例一样进行绘制,则最好的做法是使用线性轴-否则,在谈论日期时,请使用datetime轴类型。

In either case, you can use the pointStart and pointInterval properties to avoid having to send the x value in your data. 无论哪种情况,都可以使用pointStartpointInterval属性来避免在数据中发送x值。

Example: 例:

The different tick placement is because you've used 'xAxis' for the first chart and 'data' for the second. 不同的刻度位置是因为第一个图表使用了“ xAxis”,第二个图表使用了“ data”。 Change the charts so they're both using the same data method and they should look the same. 更改图表,使它们都使用相同的数据方法,并且外观应相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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