问题
I would like to create a chart like this in Highcharter to use in R.
Ideally, what I would like to do is a bar chart where the width of the bar is the spread over the amount of days for which the event has happened. I have tried using "pointWidth", however, I need the bar width to be variable depending on the length of the event.
Here is the dataframe I am trying to plot:
df <- data.frame(event = c("wedding", "party", "concert"),
start_date = as.Date(c("2017-10-01", "2017-11-01", "2017-11-20")),
end_date = as.Date(c("2017-10-10", "2017-11-18","2017-12-01")),
event_days = c(9,17,11),
customers = c("400", "150", "3000"))
Here is how I would normally do it in ggplot:
ggplot(df, aes(start_date, customers, width = event_days)) + geom_bar(stat = "identity")
What I am trying to do is technically getting the width of the bars to be over the days over which the event took place.
This is what I would like to achieve - x-axis to be a timeline
Highcharts.chart('container', {
chart: {
type: 'variwide'
},
title: {
text: 'Labor Costs in Europe, 2016'
},
subtitle: {
text: 'Source: <a href="http://ec.europa.eu/eurostat/web/' +
'labour-market/labour-costs/main-tables">eurostat</a>'
},
xAxis: {
type: 'category',
title: {
text: 'Column widths are proportional to GDP'
}
},
legend: {
enabled: false
},
series: [{
name: 'Labor Costs',
data: [
['Norway', 50.2, 335504],
['Denmark', 42, 277339],
['Belgium', 39.2, 421611],
['Sweden', 38, 462057],
['France', 35.6, 2228857],
['Netherlands', 34.3, 702641],
['Finland', 33.2, 215615],
['Germany', 33.0, 3144050],
['Austria', 32.7, 349344],
['Ireland', 30.4, 275567],
['Italy', 27.8, 1672438],
['United Kingdom', 26.7, 2366911],
['Spain', 21.3, 1113851],
['Greece', 14.2, 175887],
['Portugal', 13.7, 184933],
['Czech Republic', 10.2, 176564],
['Poland', 8.6, 424269],
['Romania', 5.5, 169578]
],
dataLabels: {
enabled: true,
format: '€{point.y:.0f}'
},
tooltip: {
pointFormat: 'Labor Costs: <b>€ {point.y}/h</b><br>' +
'GDP: <b>€ {point.z} million</b><br>'
},
colorByPoint: true
}]
});
#container {
max-width: 800px;
min-width: 380px;
margin: 0 auto;
}
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/variwide.js"></script>
<div id="container"></div>
回答1:
pointRange
property (that belongs to a series) is responsible for giving the columns a proper span.
Two simple solutions came to my mind:
1. Create a separate series for every point, apply pointRange
and disable grouping
: http://jsfiddle.net/BlackLabel/x8c3Ldwc/
2. Ignore pointRange
and use area series instead (separate series for every column). Some special configuration (plotOptions
in the demo) has to be applied to make it look like a column chart: http://jsfiddle.net/BlackLabel/o1o5Ly1y/
回答2:
So I have managed to translate what Kamil has provided on jsfiddle to Highcharter in the following way.
Firstly, the data has to be in a different format as opposed to the way I have described in the question.
df2 <- data.frame(start_date = as.Date(c("2017-10-01", "2017-10-10", "2017-11-01", "2017-11-02", "2017-12-01", "2017-12-15")),
customers = c(400,400, 200, 200,150,150),
event = c("wedding","wedding", "party", "party", "concert","concert"))
Secondly, the chart type has to be set to "area" instead of column:
highchart() %>%
hc_add_series(df2, "area", hcaes(x = datetime_to_timestamp(start_date),
y = customers, group = event)) %>%
hc_xAxis(type = "datetime",
title = "",
showFirstLabel = FALSE,
showLastLabel = FALSE,
enableMouseTracking = TRUE) %>%
hc_plotOptions(area = list(marker = list(
enabled = FALSE,
lineWidth = 0,
states = list(
hover = list(enabled = FALSE))))) %>%
hc_tooltip(followPointer = TRUE)
来源:https://stackoverflow.com/questions/49386265/variwide-charts-in-highcharter