如何强制 Highcharts 日期时间 X 轴上的刻度线/网格线与数据点任意对齐?
我正在使用lazy_high_charts ruby gem 制作highcharts 图表。 x 轴代表连续的日期,每隔三个就有一个标签。但是,我似乎无法控制标签出现在哪些日期下,而且无法控制网格线出现的位置。无论图表开始或结束的日期,它似乎“锁定”为将刻度分配给某些日期,而我无法找到更改它的方法。
这还有另一个烦人的副作用,因为我使用 startOnTick 和 endOnTick 设置为 true:有时数据系列线与图表的左侧齐平(在这种情况下确实与网格线/刻度线/标签对齐),并且有时它们从最左边的值的右侧开始。
我已经尝试了所有我能想到的方法,但还没有找到使事情看起来漂亮且一致的解决方案。我想要的是在图表的左侧和右侧有网格线/刻度线/标签,第一个标签与第一个数据点对齐,无论发生在哪一天。
(编辑:不幸的是,我无法发布下面的屏幕截图,因为我是新用户......如果我设法获得 10 个代表点,我会上传它们) 以下是显示两个日期范围的屏幕截图,说明了这两种情况:
如您所见,在这两种情况下,标签/刻度/网格线都落在完全相同的日期,即使时间范围不同。 (编辑:见上文)
下面是我正在使用的图表选项。抱歉,它们采用 ruby 语法,但它们会直接翻译成 javascript。除了这个问题之外,图表的显示完全按照我想要的方式显示,因此我不想进行任何会损害外观任何部分的更改。谢谢!
f.options[:title] = nil
f.options[:legend] = { :enabled => false }
f.options[:chart] = {
:defaultSeriesType => "line",
:height => 342,
:width => 760,
:opacity => 0.0,
:style => "{ width: 760px; height: 342px; }",
:borderColor => '#000000',
:plotBorderColor => '#000000',
:labels => "{ color: '#000000'; }",
:spacingLeft => 25,
:spacingRight => 25,
}
f.options[:xAxis] = {
:type => 'linear',
:gridLineWidth => 1,
:gridLineDashStyle => 'ShortDash',
:startOnTick => true,
:endOnTick => true,
:showLastLabel => true,
:tickInterval => 3.days * 1000,
:tickLength => 10,
:tickWidth => 1,
:tickPosition => 'inside',
:tickmarkPlacement => 'on',
:minorGridLineWidth => 0,
:minorGridLineDashStyle => 'Dot',
:minorTickInterval => 1.day * 1000,
:minorTickLength => 5,
:minorTickWidth => 1,
:offset => 15,
:minorTickPosition => 'inside',
:lineWidth => 0,
:labels => { :align => 'center', :step => 1 },
:min => "2012-01-09".to_datetime.to_i * 1000,
:max => "2012-01-09".to_datetime.to_i * 1000 + 29.days * 1000,
}
f.options[:yAxis] = {
:title => { :text => nil },
:type => 'linear',
:gridLineWidth => 0,
:min => 0,
:max => 100,
:labels => { :enabled => false }
}
f.series(:name=>'SeriesA', :data=>[29.9, 71.5, 54.4, 98, 2, 14.0, 23.6, 65.5, 34.4, 88.1], :color => "#D846AD", :pointStart => "2012-01-09".to_datetime.to_i * 1000, :pointInterval => 3.days * 1000)
f.series(:name=>'SeriesB', :data=> [40.9, 71.5, 32.4, 12.2, 12.0, 32.0, 43.6, 34.5, 56.4, 87.1], :color => "#0A96C9", :pointStart => "2012-01-09".to_datetime.to_i * 1000, :pointInterval => 3.days * 1000)
I'm working on a highcharts chart using the lazy_high_charts ruby gem. The x-axis represents consecutive date, with labels on every third one. However, I seem to have no control over WHICH dates the labels appear under, and along with that, no control over where the grid lines appear. Regardless of what date the chart begins or ends, it seems "locked" into assigning the ticks to certain dates and I can't figure out a way to change it.
This has another annoying side effect, since I am using startOnTick and endOnTick set to true: sometimes the data series lines appear flush with the left side of the chart (and in this case do line up with the gridlines/ticks/labels), and sometimes they begin to the right of the leftmost value.
I have tried all that I can think of and haven't found a solution that makes things look nice and consistent. What I want is to have gridlines/ticks/labels on the left and right sides of the chart, with the first label lining up with the first data point, regardless of what day that happens to be on.
(EDIT: I can't post the below screenshots, unfortunately, because I'm a new user...if I manage to get 10 rep points I'll upload them)
Here are screenshots showing two dates ranges that illustrate both scenarios:
As you can see, in both cases the labels/ticks/gridlines fall on exactly the same dates, even though the time ranges are different.
(EDIT: see above)
Below are the chart options I am using. Apologies that they are in ruby syntax, but they get translated straightforwardly into javascript. Other than this issue, the chart is appearing exactly as I want it to, so I don't want to make any changes that will compromise any part of the appearance. Thanks!
f.options[:title] = nil
f.options[:legend] = { :enabled => false }
f.options[:chart] = {
:defaultSeriesType => "line",
:height => 342,
:width => 760,
:opacity => 0.0,
:style => "{ width: 760px; height: 342px; }",
:borderColor => '#000000',
:plotBorderColor => '#000000',
:labels => "{ color: '#000000'; }",
:spacingLeft => 25,
:spacingRight => 25,
}
f.options[:xAxis] = {
:type => 'linear',
:gridLineWidth => 1,
:gridLineDashStyle => 'ShortDash',
:startOnTick => true,
:endOnTick => true,
:showLastLabel => true,
:tickInterval => 3.days * 1000,
:tickLength => 10,
:tickWidth => 1,
:tickPosition => 'inside',
:tickmarkPlacement => 'on',
:minorGridLineWidth => 0,
:minorGridLineDashStyle => 'Dot',
:minorTickInterval => 1.day * 1000,
:minorTickLength => 5,
:minorTickWidth => 1,
:offset => 15,
:minorTickPosition => 'inside',
:lineWidth => 0,
:labels => { :align => 'center', :step => 1 },
:min => "2012-01-09".to_datetime.to_i * 1000,
:max => "2012-01-09".to_datetime.to_i * 1000 + 29.days * 1000,
}
f.options[:yAxis] = {
:title => { :text => nil },
:type => 'linear',
:gridLineWidth => 0,
:min => 0,
:max => 100,
:labels => { :enabled => false }
}
f.series(:name=>'SeriesA', :data=>[29.9, 71.5, 54.4, 98, 2, 14.0, 23.6, 65.5, 34.4, 88.1], :color => "#D846AD", :pointStart => "2012-01-09".to_datetime.to_i * 1000, :pointInterval => 3.days * 1000)
f.series(:name=>'SeriesB', :data=> [40.9, 71.5, 32.4, 12.2, 12.0, 32.0, 43.6, 34.5, 56.4, 87.1], :color => "#0A96C9", :pointStart => "2012-01-09".to_datetime.to_i * 1000, :pointInterval => 3.days * 1000)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在没有看到屏幕截图的情况下,很难判断这是否是您遇到的确切问题,但如果您的问题正是我所认为的,那么这里是解决方案:
尝试设置
minPadding
和相关轴的 maxPadding
为 0,以及startOnTick: true
和endOnTick: true
。如果您不设置填充,则可能会覆盖startOnTick
和endOnTick
的行为。It's hard to tell if this is the exact issue that you're having without seeing the screenshots, but if your problem is what I think it is, here is the solution:
Try setting
minPadding
andmaxPadding
to 0 for the relevant axis along withstartOnTick: true
andendOnTick: true
. Supposedly the behavior ofstartOnTick
andendOnTick
gets overridden if you don't also set the padding.我认为你的问题是日期时间标签的自动调整。
Highcharts 将获取某个点的日期并根据其自身的逻辑设置轴标签。
例如,如果您的轴跨越几年,它可能会显示 2012 年 8 月甚至仅显示年份。
因此,您所看到的将日期 01.Aug 2012 分配给现实中的轴,Highcharts 正在显示适合轴刻度的日期表示形式。
您可以通过设置 xAxis 的 DateTimeLabelFormat 来控制此行为:
参见 HighCharts 参考
所以您所要做的就是更改this:
到类似的东西
来让它显示:
例如,对于一天或以上的轴刻度,它会显示 23. Jan 为您的点;对于一年以上的刻度,它会显示 23. Jan 2012。
另一种方法是强制图表显示您的值。我认为这在日期时间折线图中是不可取的,但这里有一个例子:
JSFiddle
这里是StackOverflow 问题,更详细一些
I think your problem is the automatic adjustment of the datetime labels.
Highcharts will take the date of a point and set the axis label according to its own logic.
For example if your axis spans several years it may display Aug 2012 or even just the year.
So what you see as assigning the date 01.Aug 2012 to the axis in reality Highcharts is displaying a date representation that it finds suitable for the axis scale.
You can control this behavior by setting the DateTimeLabelFormat for your xAxis:
see HighCharts Reference
So all you have to do is change this:
to something like
to get it to display:
So for example it will display 23. Jan for your point for an axis scale of day or above and 23. Jan 2012 for a scale over a year.
Another Method would be to force the chart to display your values. I don't think that is advisable in a datetime line chart but here is an example:
JSFiddle
And here the StackOverflow question with a bit more detail