使用下拉过滤器动态更新Highchart
目前,我试图在图表上添加一个下拉过滤器按钮,让用户从数据属性中选择特定值并过滤图表上显示的相关数据。
我将数据的属性填充到选择单元,并且尝试通过从选择值解析数据来更新图表,但似乎数据没有在 HighChart 上更新。
这是我的代码: jsfiddle
const data = [
{
"Data": "aaa",
"Media": "1",
"Row": "1",
"Column": "3",
"Code": "24",
},
{
"Data": "aaa",
"Media": "2",
"Row": "1",
"Column": "1",
"Code": "24",
},
{
"Data": "aaa",
"Media": "3",
"Row": "1",
"Column": "3",
"Code": "24",
},
{
"Data": "aaa",
"Media": "4",
"Row": "1",
"Column": "2",
"Code": "24",
},
{
"Data": "aaa",
"Media": "1",
"Row": "2",
"Column": "1",
"Code": "24",
},
// populate Media to dropdown list
$.each(data, function (index, record) {
$('<option>', {
value: record.Media,
text: record.Media
}).appendTo("#select_media");
});
// remove duplicate value from dropdown list
var optionValues =[];
$('#select_media option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove()
}else{
optionValues.push(this.value);
}
});
// getting drop down list value
var mediaselect = document.getElementById("select_media").value
mediaselect_str = String(mediaselect)
const parsedData = []
data.forEach(obj => {
if (obj.Media == mediaselect_str) {
parsedData.push({
x: parseInt(obj.Row),
y: parseInt(obj.Column),
label: [obj.Data, obj.Media, obj.Code]
})
}
});
var chart = Highcharts.chart('tray_container', {
chart: {
type: 'heatmap',
width: 500,
height: 500,
},
title: {
text: null
},
plotOptions: {
heatmap: {
borderColor: "black",
paddingRight: 100
},
},
series: [{
name: null,
borderWidth: 1,
data: parsedData,
dataLabels: {
inside: true,//Show
enabled: true,
useHTML: true,
opacity: 1,
verticalAlign:'middle',
align: 'center',
style: {
textShadow: 'none',
HcTextStroke: null,
display: 'block'
},
formatter: function() {
const label = this.point.label;
return `${label[1]} <br/> ${label[2]} <br/> ${label[0]}`
}
},
}]
});
我做错了什么吗?谢谢。
=================================================== ===============
更新:JMS 的解决方案工作正常,但在我的实际情况中,我的 json 数据是从某个地方调用的,所以这将是一个函数。根据 JMS 的方法,结果是当我从下拉列表中进行选择时,它找不到 update() 函数,因为它嵌套在我的函数中。 抱歉,我对 javascript 还很陌生,我不知道如何解决它。
function generate_chart(data) {
// populate Media to dropdown list
$.each(data, function (index, record) {
$('<option>', {
value: record.Media,
text: record.Media
}).appendTo("#select_media");
});
// remove duplicate value from dropdown list
var optionValues =[];
$('#select_media option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove()
}else{
optionValues.push(this.value);
}
});
update();
function update() {
// find the maximun of x & y values by row and column
var max_x = Math.max.apply(Math, fildata.map(function(mx) {
return mx.OutputRow;
}))
var max_y = Math.max.apply(Math, fildata.map(function(my) {
return my.OutputColumn;
}))
console.log(max_x, max_y)
// assign x & y axis array with the maxiumn
var x = Array(max_x).fill().map((element, index) => index)
var y = Array(max_y).fill().map((element, index) => index)
// getting drop down list value
var mediaselect = document.getElementById("select_media").value
mediaselect_str = String(mediaselect)
const parsedData = []
data.forEach(obj => {
if (obj.Media == mediaselect_str) {
parsedData.push({
x: parseInt(obj.Row),
y: parseInt(obj.Column),
label: [obj.Data, obj.Media, obj.Code]
})
}
});
var chart = Highcharts.chart('tray_container', {
chart: {
type: 'heatmap',
width: 500,
height: 500,
},
title: {
text: null
},
plotOptions: {
heatmap: {
borderColor: "black",
paddingRight: 100
},
},
series: [{
name: null,
borderWidth: 1,
data: parsedData,
dataLabels: {
inside: true,//Show
enabled: true,
useHTML: true,
opacity: 1,
verticalAlign:'middle',
align: 'center',
style: {
textShadow: 'none',
HcTextStroke: null,
display: 'block'
},
formatter: function() {
const label = this.point.label;
return `${label[1]} <br/> ${label[2]} <br/> ${label[0]}`
}
},
}]
});
}
currently I was trying to add a dropdown filter button on my chart to let users select a specific values from a data attribute and filter the related data shown on the chart.
I populated my data's attribute to the select unit, and I was trying to update the chart with parse the data from the selective value, but seems the data didn't update on the HighChart.
Here is my code:
jsfiddle
const data = [
{
"Data": "aaa",
"Media": "1",
"Row": "1",
"Column": "3",
"Code": "24",
},
{
"Data": "aaa",
"Media": "2",
"Row": "1",
"Column": "1",
"Code": "24",
},
{
"Data": "aaa",
"Media": "3",
"Row": "1",
"Column": "3",
"Code": "24",
},
{
"Data": "aaa",
"Media": "4",
"Row": "1",
"Column": "2",
"Code": "24",
},
{
"Data": "aaa",
"Media": "1",
"Row": "2",
"Column": "1",
"Code": "24",
},
// populate Media to dropdown list
$.each(data, function (index, record) {
$('<option>', {
value: record.Media,
text: record.Media
}).appendTo("#select_media");
});
// remove duplicate value from dropdown list
var optionValues =[];
$('#select_media option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove()
}else{
optionValues.push(this.value);
}
});
// getting drop down list value
var mediaselect = document.getElementById("select_media").value
mediaselect_str = String(mediaselect)
const parsedData = []
data.forEach(obj => {
if (obj.Media == mediaselect_str) {
parsedData.push({
x: parseInt(obj.Row),
y: parseInt(obj.Column),
label: [obj.Data, obj.Media, obj.Code]
})
}
});
var chart = Highcharts.chart('tray_container', {
chart: {
type: 'heatmap',
width: 500,
height: 500,
},
title: {
text: null
},
plotOptions: {
heatmap: {
borderColor: "black",
paddingRight: 100
},
},
series: [{
name: null,
borderWidth: 1,
data: parsedData,
dataLabels: {
inside: true,//Show
enabled: true,
useHTML: true,
opacity: 1,
verticalAlign:'middle',
align: 'center',
style: {
textShadow: 'none',
HcTextStroke: null,
display: 'block'
},
formatter: function() {
const label = this.point.label;
return `${label[1]} <br/> ${label[2]} <br/> ${label[0]}`
}
},
}]
});
Is there anything I did wrong? Thank you.
=================================================================
Updated: Solution from JMS work prefectly, but in my real case my json data was called from somewhere so this will be a function. Based on JMS's method the result was while I make selection from dropdown it counldn't find update() function since it was nested inside my function.
Sorry I'm still new in the javascript and I can't figure out how to solve it.
function generate_chart(data) {
// populate Media to dropdown list
$.each(data, function (index, record) {
$('<option>', {
value: record.Media,
text: record.Media
}).appendTo("#select_media");
});
// remove duplicate value from dropdown list
var optionValues =[];
$('#select_media option').each(function(){
if($.inArray(this.value, optionValues) >-1){
$(this).remove()
}else{
optionValues.push(this.value);
}
});
update();
function update() {
// find the maximun of x & y values by row and column
var max_x = Math.max.apply(Math, fildata.map(function(mx) {
return mx.OutputRow;
}))
var max_y = Math.max.apply(Math, fildata.map(function(my) {
return my.OutputColumn;
}))
console.log(max_x, max_y)
// assign x & y axis array with the maxiumn
var x = Array(max_x).fill().map((element, index) => index)
var y = Array(max_y).fill().map((element, index) => index)
// getting drop down list value
var mediaselect = document.getElementById("select_media").value
mediaselect_str = String(mediaselect)
const parsedData = []
data.forEach(obj => {
if (obj.Media == mediaselect_str) {
parsedData.push({
x: parseInt(obj.Row),
y: parseInt(obj.Column),
label: [obj.Data, obj.Media, obj.Code]
})
}
});
var chart = Highcharts.chart('tray_container', {
chart: {
type: 'heatmap',
width: 500,
height: 500,
},
title: {
text: null
},
plotOptions: {
heatmap: {
borderColor: "black",
paddingRight: 100
},
},
series: [{
name: null,
borderWidth: 1,
data: parsedData,
dataLabels: {
inside: true,//Show
enabled: true,
useHTML: true,
opacity: 1,
verticalAlign:'middle',
align: 'center',
style: {
textShadow: 'none',
HcTextStroke: null,
display: 'block'
},
formatter: function() {
const label = this.point.label;
return `${label[1]} <br/> ${label[2]} <br/> ${label[0]}`
}
},
}]
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您从未订阅下拉列表的更改,因此数据不会更改。
在下拉列表中添加 onchange 侦听器
将更新逻辑放入
function update()
函数update()
一次,以获取初始图表请参阅此处的工作小提琴:http://jsfiddle.net/ran2vz7s/3/
You never subscribed to changes of the dropdown, so the data will not change.
Add an onchange listener on the dropdown
Put your update logic into the
function update()
functionupdate()
once yourself right at the start, to get the initial chartSee the working fiddle here: http://jsfiddle.net/ran2vz7s/3/
update() 将是不错的选择,下面是演示如何使用选择元素更改图表数据。
演示: https://jsfiddle.net/BlackLabel/m84f3spx/
API 参考:
https://api.highcharts.com/class-reference/Highcharts.Chart#更新
The update() will be good options, under is demo showing how to change chart data with select element.
Demo: https://jsfiddle.net/BlackLabel/m84f3spx/
API References:
https://api.highcharts.com/class-reference/Highcharts.Chart#update