0
我想在圖表中的數據集頂部數據點顯示工具提示。這樣用戶就可以看到最大和最小數據點的值。這有助於立即理解圖表。ChartJS以最大值/最小值顯示工具提示
我已經在這裏創建的jsfiddle與代碼:https://jsfiddle.net/h2zscw6s/1/
僅供參考,請在下面也找到我的圖表配置。這是在咖啡腳本,但我不介意,如果你張貼在純JavaScript的答案。
dataWeather = [
{
label: 'January'
value: 22
}
{
label: 'February'
value: 23
}
{
label: 'March'
value: 24
}
{
label: 'May'
value: 26
}
{
label: 'June'
value: 30
}
{
label: 'July'
value: 34
}
{
label: 'August'
value: 38
}
{
label: 'September'
value: 36
}
{
label: 'October'
value: 30
}
{
label: 'November'
value: 28
}
{
label: 'December'
value: 26
}
]
dataPrices = [
{
label: 'January'
value: 5000
}
{
label: 'February'
value: 4500
}
{
label: 'March'
value: 4450
}
{
label: 'May'
value: 3700
}
{
label: 'June'
value: 3700
}
{
label: 'July'
value: 3000
}
{
label: 'August'
value: 2900
}
{
label: 'September'
value: 3100
}
{
label: 'October'
value: 3200
}
{
label: 'November'
value: 3900
}
{
label: 'December'
value: 5500
}
]
class WeatherPriceChart
setWeatherData: (weatherData)->
@weatherData = weatherData
setPriceData: (priceData)->
@priceData = priceData
minPrice: ->
_.sortBy(@priceData, (w)-> w.value)[0]?.value || 0
maxPrice: ->
_.sortBy(@priceData, (w)-> -w.value)[0]?.value || 0
minTemperature: ->
_.sortBy(@weatherData, (w)-> w.value)[0]?.value || 0
maxTemperature: ->
_.sortBy(@weatherData, (w)-> -w.value)[0]?.value || 0
isMaxTemperature: (value)->
@maxTemperature() == value
isMinTemperature: (value)->
@minTemperature() == value
isMaxPrice: (value)->
@maxPrice() == value
isMinPrice: (value)->
@minPrice() == value
getLabels: ->
_.map(@weatherData, (w)-> w.label)
getWeatherDataPoints: ->
_.map(@weatherData, (w)-> w.value)
getPriceDataPoints: ->
_.map(@priceData, (w)-> w.value)
getNormalizedWeatherDataPoints: ->
data = @weatherData
min = -10
max = 60
_.map data, (d)->
norm = d.value + (min * -1)
maxed = max + (min * -1)
norm/maxed * 100
getNormalizedPriceDataPoints: ->
data = @priceData
max = @maxPrice() * 2.5
_.map data, (d)->
d.value/max * 100
chart = new WeatherPriceChart
chart.setWeatherData(dataWeather)
chart.setPriceData(dataPrices)
ctx = document.getElementById('myChart')
myChart = new Chart(ctx,
type: 'line',
data:
xLabels: chart.getLabels(),
yLabels: [""],
datasets: [
{
label: 'Temperature'
data: chart.getWeatherDataPoints()
backgroundColor: 'rgba(239,88,42,0.2)'
borderColor: 'rgba(239,88,42,0.5)'
borderWidth: 1
},
{
label: 'Prices'
data: chart.getNormalizedPriceDataPoints()
backgroundColor: 'rgba(22,195,245,0.2)'
borderColor:'rgba(22,195,245,0.4)'
borderWidth: 1
}
]
options:
scales:
yAxes: [
{
ticks: {
beginAtZero: false,
display: false
},
display: false
},
]
legend:
display: false
)