我有"类型"领域。
对于所选的值" dataDaily"我有四种类型选项:" spline"," area"," line"," bar"。
那么我应该怎样做以便为"类型"字段。
供参考: http://jsfiddle.net/km97cdL3/
请提出一些解决方案。
HTML:
<html>
<select id="DataType">
<option value="dataDaily">Daily</option>
<option value="dataWeekly">Weekly</option>
<option value="dataMonthly">Monthly</option>
</select>
<select id="chartType">
<option value="spline">spline chart</option>
<option value="area">area chart</option>
<option value="line">line chart</option>
<option value="bar">bar chart</option>
</select>
</html>
JS:
$(function () {
var chart = c3.generate({
bindto:'#chart',
data: {
x : 'x',
columns: dataDaily,
type: 'spline',
labels:true
},
axis: {
x: {
type: 'category' // this needed to load string x value
}
}
});
$("#DataType").change(function (evt) {
var timeSelection = eval( $("#DataType").val());
var chart = c3.generate({
bindto:'#chart',
data: {
x : 'x',
columns: timeSelection,
type: 'spline',
labels:true
},
axis: {
x: {
type: 'category' // this needed to load string x value
}
}
});
});
答案 0 :(得分:2)
您需要使用闭包内的某个变量跟踪更新,并在更新图表时使用它们(currentData,currentType):
$(function () {
var currentData = dataDaily;
var currentType = 'spline';
var chart = c3.generate({
bindto:'#chart',
data: {
x : 'x',
columns: currentData,
type: currentType,
labels:true
},
axis: {
x: {
type: 'category' // this needed to load string x value
}
}
});
$("#DataType").change(function (evt) {
var timeSelection = eval( $("#DataType").val());
currentData = timeSelection;
var chart = c3.generate({
bindto:'#chart',
data: {
x : 'x',
columns: currentData,
type: currentType,
labels:true
},
axis: {
x: {
type: 'category' // this needed to load string x value
}
}
});
$("#chartType").change(function (evt) {
var chartSelection = $("#chartType").val();
currentType = chartSelection;
var chart = c3.generate({
bindto:'#chart',
data: {
x : 'x',
columns: currentData,
type: currentType,
labels:true
},
axis: {
x: {
type: 'category' // this needed to load string x value
}
}
});
});