jquery - 在初始渲染后使用jquery更新highcharts图表对象的轴(django)
问题描述
我似乎可以弄清楚一个基本问题
我的 django 应用程序中有一个 highcharts 图表,该图表正在加载到模板中。图表的数据是通过 ajax 填充的。
我想在图表上方添加一个按钮,如果单击该按钮,则会将轴切换为对数(而不是默认的线性),但我似乎无法完成这项工作
这是我的代码
HTML
<button id="target" type='button' > switch to log </button>
<div id="container1" style="width:100%; height:400px;"></div>
Javascript
<script>
$(document).ready(function() {
var chartOptions = {
chart: {
renderTo: 'container1',
type: 'line',
},
title: {text: null},
xAxis: {title: {text: null}, labels: {rotation: -45}},
yAxis: {title: {text: 'Item counts'},
type: 'linear',
stackLabels: {enabled: true},
},
plotOptions: {column: {dataLabels: {enabled: false}, stacking: 'normal'}},
series: [{}],
};
var chartDataUrl = "{% url 'global_trend_chart' %}" ;
$.getJSON(chartDataUrl,
function(data) {
chartOptions.xAxis.categories = data['chart_data']['date'];
chartOptions.series[0].name = 'Global counts';
chartOptions.series[0].data = data['chart_data']['item_counts'];
var chartA = new Highcharts.Chart(chartOptions);
});
$( "#target" ).click(function() {
chartA.update({
yAxis: {
type: 'logarithmic',
}
});
});
} );
</script>
我很感激帮助
解决方案
chartA
点击回调函数范围内不存在Tha变量。您需要在更高级别上声明它,例如:
var chartDataUrl = "{% url 'global_trend_chart' %}";
var chartA;
$.getJSON(chartDataUrl,
function(data) {
chartOptions.xAxis.categories = data['chart_data']['date'];
chartOptions.series[0].name = 'Global counts';
chartOptions.series[0].data = data['chart_data']['item_counts'];
chartA = new Highcharts.Chart(chartOptions);
});
$( "#target" ).click(function() {
chartA.update({
yAxis: {
type: 'logarithmic',
}
});
});
现场演示:http: //jsfiddle.net/BlackLabel/6m4e8x0y/4935/
推荐阅读
- github - Dependabot 没有找到最新的提交
- linux-kernel - 如何向/从内核模块传递/读取配置数据,例如/etc/file?
- github - 如何使用 Github GUI 恢复到以前的提交?
- python - 如果用户有一些角色 discord.py,我正在尝试做一些事情
- asp.net - Fedex API 集成 - 如何访问退货标签图像?
- regex - 在地址表单中限制邮政信箱地址
- java - 我试图找到两点之间的距离
- excel - 根据范围“F”值插入行后从范围“H”复制一个值
- python - 反映加密的 Access 数据库时出现“无效密码”错误
- python - Groupby 并将特定函数应用于某些列并获取 df Pandas 的第一个或最后一个值