获取在 FLOT 图表上显示的默认图表
我正在使用这个特定的 FLOT 示例来使用我们的一些数据构建图表:
http: //people.iola.dk/olau/flot/examples/ajax.html
我稍微修改了标记 - 我没有按钮,而是各种 JSON 文件的复选框,以这种方式:
<span>
<input class="fetchSeries" type="checkbox"> Service One
<a href="@Server.MapPath("~/Scripts/flot/servicedataone.json")"></a>
</span>
我想得到图表页面加载时预填充 JSON 文件之一。我尝试过各种方法,包括在页面加载时默认选中其中一个复选框,但没有成功。
任何帮助表示赞赏!代码如下:
<script type="text/javascript">
$(document).ready(function () {
var options = {
lines: { show: true },
points: { show: true },
yaxis: {
},
xaxis: {
mode: "time"
},
grid: { hoverable: true }
};
var data = [];
var placeholder = $("#placeholder");
$.plot(placeholder, data, options);
// fetch one series, adding to what we got
var alreadyFetched = {};
$("input.fetchSeries").click(function () {
var button = $(this);
// find the URL in the link right next to us
var dataurl = button.siblings('a').attr('href');
// then fetch the data with jQuery
function onDataReceived (series) {
// extract the first coordinate pair so you can see that
// data is now an ordinary Javascript object
var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')';
//button.siblings('span').text('Fetched ' + series.label + ', first point: ' + firstcoordinate);
// let's add it to our current data
if (!alreadyFetched[series.label]) {
alreadyFetched[series.label] = true;
data.push(series);
}
// and plot all we got
$.plot(placeholder, data, options);
}
$.ajax({
url: dataurl,
method: 'GET',
dataType: 'json',
success: onDataReceived
});
});
});
</script>
I'm using this particular FLOT example to build charts with some of our data:
http://people.iola.dk/olau/flot/examples/ajax.html
I've modified the markup a bit - instead of buttons, I have checkboxes for the various JSON files, in this manner:
<span>
<input class="fetchSeries" type="checkbox"> Service One
<a href="@Server.MapPath("~/Scripts/flot/servicedataone.json")"></a>
</span>
I would like to have the graph pre-populate with one of the JSON files when the page loads. I've tried various things, including having one of the checkboxes checked by default when the page loads, but no luck.
Any help is appreciated! Code below:
<script type="text/javascript">
$(document).ready(function () {
var options = {
lines: { show: true },
points: { show: true },
yaxis: {
},
xaxis: {
mode: "time"
},
grid: { hoverable: true }
};
var data = [];
var placeholder = $("#placeholder");
$.plot(placeholder, data, options);
// fetch one series, adding to what we got
var alreadyFetched = {};
$("input.fetchSeries").click(function () {
var button = $(this);
// find the URL in the link right next to us
var dataurl = button.siblings('a').attr('href');
// then fetch the data with jQuery
function onDataReceived (series) {
// extract the first coordinate pair so you can see that
// data is now an ordinary Javascript object
var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')';
//button.siblings('span').text('Fetched ' + series.label + ', first point: ' + firstcoordinate);
// let's add it to our current data
if (!alreadyFetched[series.label]) {
alreadyFetched[series.label] = true;
data.push(series);
}
// and plot all we got
$.plot(placeholder, data, options);
}
$.ajax({
url: dataurl,
method: 'GET',
dataType: 'json',
success: onDataReceived
});
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样 - 设置
$("input.fetchSeries").click
后,通过调用$("input.fetchSeries:first").click()
触发它代码>.您需要修改:first
部分以匹配您实际想要设置的复选框。它看起来像这样: http://jsfiddle.net/ryleyb/bdSrc/
How about this - after you setup your
$("input.fetchSeries").click
, trigger it, by calling$("input.fetchSeries:first").click()
. You'll want to modify the:first
part to match whichever checkbox you actually want to set off.It would look like this: http://jsfiddle.net/ryleyb/bdSrc/