Add another series to existing plot with flot

让人想犯罪 __ 提交于 2020-01-06 06:03:08

问题


I need to know how I can easily add another series to an existing plot using Flot.

Here is how I currently plot a single series:

function sendQuery() {
    var host_name = $('#hostNameInput').val();
    var objectName = $('#objectNameSelect option:selected').text();
    var instanceName = $('#instanceNameSelect option:selected').text();
    var counterName = $('#counterNameSelect option:selected').text();
    $.ajax({
        beforeSend: function () {
            $('#loading').show();
        },
        type: "GET",
        url: "http://okcmondev102/cgi-bin/itor_PerfQuery.pl?machine=" + host_name + "&objectName=" + objectName + "&instanceName=" + instanceName + "&counterName=" + counterName,
        dataType: "XML",
        success: function (xml) {
            var results = new Array();
            var counter = 0;


            var $xml = $.xmlDOM(xml);
            $xml.find('DATA').each(function () {
                results[counter] = new Array(2);
                results[counter][0] = $(this).find('TIMESTAMP').text();
                results[counter][1] = $(this).find('VALUE').text();
                counter++;
            });

            plot = $.plot($("#resultsArea"), [{
                data: results,
                label: host_name
            }], {
                series: {
                    lines: {
                        show: true
                    }
                },
                xaxis: {
                    mode: "time",
                    timeformat: "%m/%d/%y %h:%S%P"
                },
                colors: ["#000099"],
                crosshair: {
                    mode: "x"
                },
                grid: {
                    hoverable: true,
                    clickable: true
                }

            });

回答1:


You can just add another results set:

// build two data sets
var dataset1 = new Array();
var dataset2 = new Array();

var $xml = $.xmlDOM(xml);
$xml.find('DATA').each(function(){
  // use the time stamp for the x axis of both data sets
  dataset1[counter][0] = $(this).find('TIMESTAMP').text();
  dataset2[counter][0] = $(this).find('TIMESTAMP').text();
  // use the different data values for the y axis
  dataset1[counter][1] = $(this).find('VALUE1').text();
  dataset2[counter][2] = $(this).find('VALUE2').text();
  counter++;
});

// build the result array and push the two data sets in it
var results = new Array();
results.push({label: "label1", data: dataset1});
results.push({label: "label2", data: dataset2});

// display the results as before
plot = $.plot($("#resultsArea"), results, {
  // your display options
});



回答2:


At a high-level, the result of your call into itor_PerfQuery.pl will need to be extended to include the additional series data. You'll then want to make your "results" variable a multi-dimensional array to support the additional data and you'll need to update the current xml "find" loop which populates results accordingly. The remainder of the code should stay the same as flot should be able to plot the extended dataset. I think a review of the flot example will help you out. Best of luck.



来源:https://stackoverflow.com/questions/3426648/add-another-series-to-existing-plot-with-flot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!