简体   繁体   English

当 x 轴采用时间格式 (HH:MM) 时,Highchart 的热图 plot 无法正确渲染

[英]Highchart's heat-map plot not rendering correctly when x-axis is in time format (HH:MM)

I want to plot a heatmap for my data using highchart js plotting library in R -我想使用 R 中的highchart js绘图库为我的数据创建一个heatmap plot -

library(highcharter)
library(dplyr)
library(chron)
library(viridisLite )


fntltp <- JS("function(){
  return this.series.yAxis.categories[this.point.y] + this.point.x + ':' + ':<br>' +
  Highcharts.numberFormat(this.point.value, 4);
}")

Data = data.frame(Time1 = seq(as.POSIXct('2020-04-30 00:00:00', tz = "UTC"), as.POSIXct('2020-05-06 00:00:00', tz = "UTC"), by = '10 min')) %>%
           mutate(Date = paste(substr(weekdays(as.Date(Time1, tz = "UTC")), 1, 3), format(as.Date(Time1, tz = "UTC"), "%b,%d"), " "),
                'Value' = runif(865, -1, 1),
                Time = times(format(Time1, "%H:%M:%S"))) 

hchart(Data, "heatmap", hcaes(x = Time, y = Date, value = Value)) %>% 
  hc_colorAxis(stops = color_stops(30, rev(viridis(30)))) %>% 
  hc_yAxis(reversed = TRUE, offset = 2, tickLength = 0,
           gridLineWidth = 0, minorGridLineWidth = 0,
           labels = list(style = list(fontSize = "12px"))) %>%
  hc_xAxis(labels = list(type = 'datetime', dateTimeLabelFormats = list(day = '%H:%M'))) %>%
  hc_legend(layout = "vertical", verticalAlign = "middle",
            align = "right", valueDecimals = 0) %>% 
  hc_size(height = 800) %>% 
  hc_tooltip(formatter = fntltp)

Clearly this is not the correct plot, when my x-axis is formatted in HH:MM (in time).显然这不是正确的 plot,当我的x-axis格式为HH:MM (及时)时。 Additionally in the legend too, the x-axis values are not flowing in correctly.此外,在legend中, x-axis值也没有正确流入。

Furthermore, the order of y-axis is also not smooth, as I wanted to have the latest day in the bottom and the first day at the top.此外, y-axis的顺序也不平滑,因为我希望最近一天在底部,第一天在顶部。

Any pointer how to correct those irregularities will be highly helpful.任何如何纠正这些违规行为的指针都将非常有帮助。

Modified post based on @raf18seb's reply -根据@raf18seb 的回复修改后的帖子 -

Below is my new plot with simplified data -下面是我的新 plot 简化数据 -

library(highcharter)
library(dplyr)
library(chron)

set.seed(1)
Data = data.frame(Time1 = seq(as.POSIXct('2020-04-30 00:00:00', tz = "UTC"), as.POSIXct('2020-05-06 00:00:00', tz = "UTC"), by = '10 min')) %>%
           mutate(Date = paste(substr(weekdays(as.Date(Time1, tz = "UTC")), 1, 3), format(as.Date(Time1, tz = "UTC"), "%b,%d"), " "),
                            'Value' = runif(865, -1, 1),
                    Day_Time = as.character(times(format(Time1, "%H:%M:%S"))))

tail(Data)

hchart(Data, "heatmap", hcaes(x = Day_Time, y = Date, value = Value)) %>% 
  hc_yAxis(reversed = TRUE, offset = 2, tickLength = 0,
           gridLineWidth = 0, minorGridLineWidth = 0,
           labels = list(style = list(fontSize = "12px"))) %>%
  hc_xAxis(labels = list(type = 'datetime', dateTimeLabelFormats = list(day = '%H:%M'))) %>%  
  hc_size(height = 800)

With this I want to achieve below -有了这个我想在下面实现 -

  1. In the x-axis, I want to show only HH:MM instead of HH:MM:SS .在 x 轴上,我只想显示HH:MM而不是HH:MM:SS
  2. The y-axis should be sorted in increasing order of day . y 轴应按increasing order of day排序。 ie May-6 should come at bottom and April-30 should be at top即 5 月 6 日应该在底部,4 月 30 日应该在顶部
  3. Also want to change the colour gradient ie colour should start from red (for -1) and end at green (+1) with yellow for midpoint (0)还想更改颜色渐变,即颜色应从red (for -1) ,以green (+1)结束, yellow for midpoint (0)
  4. It would also be good if I can customise the tooltip.如果我可以自定义工具提示,那也很好。 Instead of default show here, my ideal tooltip should be <Value: data-value><br><Date: date as in y-axis><br><Time: Time as in x-axis in HH:MM format>我理想的工具提示应该是<Value: data-value><br><Date: date as in y-axis><br><Time: Time as in x-axis in HH:MM format>

Any pointer how to achieve them would be highly helpful任何如何实现它们的指针都会非常有帮助

Short answer: there is a problem with your data.简短的回答:您的数据有问题。

Explanation: I managed to get your data out of your R chart.说明:我设法从您的 R 图表中取出您的数据。 You have 865 points and, after simplifying, your data looks like:您有 865 个点,简化后,您的数据如下所示:

{x: 0.00694444444444444, y: 0, value: 0.28064949112013},
{x: 0.0138888888888889, y: 0, value: -0.600626351311803},
{x: 0.0208333333333333, y: 0, value: 0.30516293970868},
...
{x: 0.166666666666667, y: 4, value: -0.619758530985564},
...

Your x values are not integers - that is why your chart looks and behaves creepy when you hover over it.您的 x 值不是整数 - 这就是为什么当您在其上使用 hover 时您的图表看起来和行为令人毛骨悚然的原因。

This is a JavaScript simplified demo with your data: https://jsfiddle.net/BlackLabel/5s3gft6u/ And this is a simple example with decimal x data to help you understand how it behaves: https://jsfiddle.net/BlackLabel/4j29o3Lh/ This is a JavaScript simplified demo with your data: https://jsfiddle.net/BlackLabel/5s3gft6u/ And this is a simple example with decimal x data to help you understand how it behaves: https://jsfiddle.net/BlackLabel/ 4j29o3Lh/

So, if I understood your issue correctly, the problem is with your data (x values) and you need to provide different data if you want to make it work in Highcharts.因此,如果我正确理解了您的问题,那么问题出在您的数据(x 值)上,如果您想让它在 Highcharts 中工作,您需要提供不同的数据。 I don't know what data exactly because I don't know what you want to display.我不知道究竟是什么数据,因为我不知道你想显示什么。

Additionally, you can try with https://api.highcharts.com/highcharts/series.heatmap.colsize此外,您可以尝试使用https://api.highcharts.com/highcharts/series.heatmap.colsize

Maybe setting it to 0.01 will meet your requirement.也许将其设置为 0.01 将满足您的要求。

edit to your new requirements:编辑您的新要求:

  1. Again, there is a problem with your data.同样,您的数据存在问题。 In JavaScript with proper data, you can format xAxis labels in many ways eg having this.value in labels.formatter The problem is that Highcharter (or R) parses your data somehow and this.value is a more complicated object without timestamp value.在 JavaScript 中使用适当的数据,您可以通过多种方式格式化 xAxis 标签,例如在labels.formatter中有 this.value 问题是 Highcharter(或 R)以某种方式解析您的数据,而 this.value 是一个更复杂的 object 没有时间戳值。 I don't know how to parse it..我不知道如何解析它..

  2. Again - your data.再次 - 您的数据。 You have points April-30 with y: 0 and May-3 with y: 1 .您在 April-30 的y: 0和 May-3 的y: 1中有积分。 In Highcharts, y value matters.在 Highcharts 中, y值很重要。 If you want to have a proper order, then you need to parse your data and match the proper date with y .如果你想有一个正确的顺序,那么你需要解析你的数据并将正确的日期与y匹配。

  3. You can use tooltip.formatter to define whatever you want to display in the tooltip您可以使用tooltip.formatter定义要在工具提示中显示的任何内容

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

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