谷歌图表数据格式错误(javascript)

时间:2014-08-11 03:24:12

标签: java javascript jsf google-visualization

我用google.visualization.arrayToDataTable(loadGraphData);调用散点图 其中loadGraphData是以ArrayList<ArrayList<String>>的形式从支持bean生成的,如下所示:

"[['Date','Variable'], [0, 9.66], [1, 9.24], [2, 8.73], [3, 8.50]...]"

数据不是使用var加载图形,但是如果我将输出粘贴到其位置(没有引号),则代码可以正常显示数据。有没有方便的方法来删除引号,或者我采取了错误的方法?

顺便说一句,我已经用0,1替换了日期......因为这导致了一个不同的错误,我还没有解决。

数据来自JSF支持bean,摘录如下:

public ArrayList graphData(int variable, int nodeNumber)
    {
    Collection dataValues =  dataValuesFacade.findVariable(currentNodes.getCurrentNodes().get(nodeNumber), variable);
    ArrayList <DataValues> dataValuesArray = new ArrayList();
    dataValuesArray.addAll(dataValues);
    dataValues =  dataValuesFacade.findVariable(currentNodes.getCurrentNodes().get(nodeNumber), 0);
    ArrayList<DataValues> timeValuesArray = new ArrayList();
    timeValuesArray.addAll(dataValues);
    ArrayList <DataPairs> dataPairs = new ArrayList();
    dataPairs.add(new DataPairs());
    ArrayList<ArrayList<String>> returnData = new ArrayList<>();
    ArrayList<String> newAL = new ArrayList();
    newAL.add("Date");
    newAL.add("Variable");
    returnData.add(newAL);
    //Gson gson = new Gson();

    for (int i=0;i<dataValuesArray.size();i++)
    {

        String varTimestamp = dataValuesArray.get(i).getdTimeStamp().toString();
        String dateTimestamp = timeValuesArray.get(i).getdTimeStamp().toString();
        String tempVariable = dataValuesArray.get(i).getdVariable().toString();
        String tempDate = timeValuesArray.get(i).getdVariable().toString();


        if ((varTimestamp.equals(dateTimestamp))&
                (dataPairs.get(i).getXdateStamp()==null)&
                (dataPairs.get(i).getYvariable()==null)&
                (dataPairs.get(i).getTimeStamp()==null))
        {
            dataPairs.get(i).setTimeStamp(dateTimestamp);
            dataPairs.get(i).setYvariable(tempVariable);
            dataPairs.get(i).setXdateStamp(tempDate);
            ArrayList<String> tempAL = new ArrayList();
            tempAL.add(i+"");
            tempAL.add(tempVariable);
            returnData.add(tempAL);
            dataPairs.add(new DataPairs());
        }
    }
    //String rt = gson.toJson(returnData);
    return returnData;
}

它很乱,但我一直在尝试各种需要清理的解决方案。

谢谢

确切输出如下:

var loadGraphData = "[[Date, Variable], [0, 9.66], [1, 9.24], [2, 8.73], [3, 8.50], [4, 8.09], [5, 8.05], [6, 7.96], [7, 7.90], [8, 7.85], [9, 7.79], [10, 7.78], [11, 7.78], [12, 7.76], [13, 7.73], [14, 7.73], [15, 7.70], [16, 7.71], [17, 7.71], [18, 7.71], [19, 7.71], [20, 7.70], [21, 7.69], [22, 7.69], [23, 7.65], [24, 7.65], [25, 7.62], [26, 7.60], [27, 7.54], [28, 7.50], [29, 7.50], [30, 7.42], [31, 7.42], [32, 7.44], [33, 7.45], [34, 7.42], [35, 7.39], [36, 7.39], [37, 7.33], [38, 7.34], [39, 7.26], [40, 7.25], [41, 7.23], [42, 7.22], [43, 7.20], [44, 7.15], [45, 7.11], [46, 7.09], [47, 7.04], [48, 7.05], [49, 7.05], [50, 6.99], [51, 6.94], [52, 6.94], [53, 6.94], [54, 6.90], [55, 6.90], [56, 6.90], [57, 6.90], [58, 6.89], [59, 6.86], [60, 6.86], [61, 6.82], [62, 6.85], [63, 6.85], [64, 6.85], [65, 6.86], [66, 6.85], [67, 6.83], [68, 6.83], [69, 6.83], [70, 6.83], [71, 6.82], [72, 6.79], [73, 6.75], [74, 6.73], [75, 6.73], [76, 6.74], [77, 6.70], [78, 6.59], [79, 6.59], [80, 6.56], [81, 6.55], [82, 6.56], [83, 6.54], [84, 6.50], [85, 6.45], [86, 6.43], [87, 6.42], [88, 6.39], [89, 6.38], [90, 6.36], [91, 6.34], [92, 6.31], [93, 6.26], [94, 6.25], [95, 6.29], [96, 6.30], [97, 6.31], [98, 6.32], [99, 6.33], [100, 6.33], [101, 6.35], [102, 6.33], [103, 6.35], [104, 6.37], [105, 6.38], [106, 6.36], [107, 6.37], [108, 6.32], [109, 6.32], [110, 6.30], [111, 6.30], [112, 6.27], [113, 6.26], [114, 6.30], [115, 6.33], [116, 6.36], [117, 6.37], [118, 6.37], [119, 6.37], [120, 6.37], [121, 6.40], [122, 6.39], [123, 6.39], [124, 6.39], [125, 6.36], [126, 6.35], [127, 6.33], [128, 6.36], [129, 6.37], [130, 6.37], [131, 6.35], [132, 6.36], [133, 6.35], [134, 6.32], [135, 6.31], [136, 6.27], [137, 6.25], [138, 6.25], [139, 6.23], [140, 6.21], [141, 6.20], [142, 6.20], [143, 6.18], [144, 6.18], [145, 6.17], [146, 6.18], [147, 6.20], [148, 6.18], [149, 6.19], [150, 6.20], [151, 6.21], [152, 6.22], [153, 6.26], [154, 6.27], [155, 6.29], [156, 6.30], [157, 6.30], [158, 6.33], [159, 6.36], [160, 6.39], [161, 6.35], [162, 6.36], [163, 6.36], [164, 6.37], [165, 6.39], [166, 6.40], [167, 6.39], [168, 6.42], [169, 6.39], [170, 6.38], [171, 6.40], [172, 6.41], [173, 6.42], [174, 6.41], [175, 6.44], [176, 6.46], [177, 6.46], [178, 6.47], [179, 6.51], [180, 6.52], [181, 6.54], [182, 6.56], [183, 6.58], [184, 6.59], [185, 6.63], [186, 6.68], [187, 6.70], [188, 6.73], [189, 6.72], [190, 6.73], [191, 6.73], [192, 6.77], [193, 6.79], [194, 6.80], [195, 6.81], [196, 6.83], [197, 6.85], [198, 6.88], [199, 6.88], [200, 6.91], [201, 6.94], [202, 6.96], [203, 6.96], [204, 6.99], [205, 7.02], [206, 7.05], [207, 7.06], [208, 7.08], [209, 7.06], [210, 7.08], [211, 7.07], [212, 7.12], [213, 7.02], [214, 6.97], [215, 6.98], [216, 6.97], [217, 6.94], [218, 6.98], [219, 6.94], [220, 6.92], [221, 6.86], [222, 6.90], [223, 6.91], [224, 6.91], [225, 6.87], [226, 6.91], [227, 6.85], [228, 6.90], [229, 6.92], [230, 6.94], [231, 6.96], [232, 6.87], [233, 6.91], [234, 6.86], [235, 6.84], [236, 6.80], [237, 6.87], [238, 6.82], [239, 6.78], [240, 6.84], [241, 6.87], [242, 6.87], [243, 6.80], [244, 6.78], [245, 6.81], [246, 6.77], [247, 6.71], [248, 6.75], [249, 6.75], [250, 6.78], [251, 6.68], [252, 6.70], [253, 6.73], [254, 6.72], [255, 6.75], [256, 6.83], [257, 6.78], [258, 6.75], [259, 6.81], [260, 6.84], [261, 6.83], [262, 6.75], [263, 6.78], [264, 6.81], [265, 6.78], [266, 6.74], [267, 6.79], [268, 6.81], [269, 6.78], [270, 6.80], [271, 6.80], [272, 6.82], [273, 6.75], [274, 6.77], [275, 6.83], [276, 6.87], [277, 6.85], [278, 6.81], [279, 6.81], [280, 6.82], [281, 6.85], [282, 6.78], [283, 6.81], [284, 6.83], [285, 6.83], [286, 6.75], [287, 6.78], [288, 6.78], [289, 6.81], [290, 6.83], [291, 6.81], [292, 6.77], [293, 6.72], [294, 6.74], [295, 6.74], [296, 6.73], [297, 6.73], [298, 6.75], [299, 6.79], [300, 6.81], [301, 6.83], [302, 6.84], [303, 6.82], [304, 6.86], [305, 6.85], [306, 6.83], [307, 6.71], [308, 6.67], [309, 6.63], [310, 6.64], [311, 6.66], [312, 6.63], [313, 6.61], [314, 6.52], [315, 6.47], [316, 6.44], [317, 6.42], [318, 6.36], [319, 6.29], [320, 6.18], [321, 6.15], [322, 6.11], [323, 6.07], [324, 6.06], [325, 5.93], [326, 5.91], [327, 5.93], [328, 5.92], [329, 5.90], [330, 5.84], [331, 5.82], [332, 5.81], [333, 5.79], [334, 5.74], [335, 5.73], [336, 5.75], [337, 5.74], [338, 5.71], [339, 5.71], [340, 5.73], [341, 5.77], [342, 5.81], [343, 5.84], [344, 5.84], [345, 5.84], [346, 5.83], [347, 5.86], [348, 5.86], [349, 5.82], [350, 5.82], [351, 5.86], [352, 5.90], [353, 5.90], [354, 5.92], [355, 5.92], [356, 5.93], [357, 5.91], [358, 5.91], [359, 5.96], [360, 5.95], [361, 5.98], [362, 5.99], [363, 5.97], [364, 5.98], [365, 6.01], [366, 6.10], [367, 6.13], [368, 6.12], [369, 6.12], [370, 6.15], [371, 6.20], [372, 6.23], [373, 6.23], [374, 6.26], [375, 6.28], [376, 6.30], [377, 6.34], [378, 6.33], [379, 6.32], [380, 6.35], [381, 6.35], [382, 6.37], [383, 6.37], [384, 6.37], [385, 6.36], [386, 6.37], [387, 6.39], [388, 6.38], [389, 6.32], [390, 6.32], [391, 6.33], [392, 6.34], [393, 6.35], [394, 6.36], [395, 6.31], [396, 6.30], [397, 6.35], [398, 6.35], [399, 6.37], [400, 6.39], [401, 6.41], [402, 6.42], [403, 6.36], [404, 6.37], [405, 6.42], [406, 6.44], [407, 6.45], [408, 6.44], [409, 6.46], [410, 6.47], [411, 6.47], [412, 6.43], [413, 6.41], [414, 6.43], [415, 6.44], [416, 6.43], [417, 6.42], [418, 6.44], [419, 6.42], [420, 6.44], [421, 6.41], [422, 6.41], [423, 6.39], [424, 6.40], [425, 6.40], [426, 6.40], [427, 6.36], [428, 6.35], [429, 6.35], [430, 6.35], [431, 6.35], [432, 6.34], [433, 6.34], [434, 6.33], [435, 6.30], [436, 6.30], [437, 6.27], [438, 6.27], [439, 6.27], [440, 6.37], [441, 6.38], [442, 6.37], [443, 6.33], [444, 6.30], [445, 6.30], [446, 6.30], [447, 6.30], [448, 6.32], [449, 6.33], [450, 6.33], [451, 6.30], [452, 6.30], [453, 6.30], [454, 6.27], [455, 6.27], [456, 6.27], [457, 6.27], [458, 6.27], [459, 6.27], [460, 6.27], [461, 6.27], [462, 6.22], [463, 6.20], [464, 6.19], [465, 6.18], [466, 6.21], [467, 6.24], [468, 6.24], [469, 6.25], [470, 6.25], [471, 6.27], [472, 6.27], [473, 6.27], [474, 6.28], [475, 6.30], [476, 6.30], [477, 6.30], [478, 6.30], [479, 6.30], [480, 6.30], [481, 6.31], [482, 6.33], [483, 6.34], [484, 6.36], [485, 6.34], [486, 6.34], [487, 6.31], [488, 6.30], [489, 6.29], [490, 6.26], [491, 6.24], [492, 6.20], [493, 6.15], [494, 6.10], [495, 6.10], [496, 6.09], [497, 6.10], [498, 6.09], [499, 6.07], [500, 6.05], [501, 6.03], [502, 6.00], [503, 5.97], [504, 5.96], [505, 5.95], [506, 5.93], [507, 5.93], [508, 5.92], [509, 5.91], [510, 5.91], [511, 5.88], [512, 5.86], [513, 5.85], [514, 5.86], [515, 5.85], [516, 5.84], [517, 5.84], [518, 5.84], [519, 5.84], [520, 5.81], [521, 5.81], [522, 5.82], [523, 5.79], [524, 5.79], [525, 5.78], [526, 5.79], [527, 5.80], [528, 5.80], [529, 5.78], [530, 5.76], [531, 5.76], [532, 5.76], [533, 5.75], [534, 5.75], [535, 5.71], [536, 5.68], [537, 5.66], [538, 5.64], [539, 5.64], [540, 5.59], [541, 5.58], [542, 5.59], [543, 5.57], [544, 5.52], [545, 5.52], [546, 5.50], [547, 5.49], [548, 5.49], [549, 5.47], [550, 5.46], [551, 5.48], [552, 5.47], [553, 5.47], [554, 5.48], [555, 5.50], [556, 5.52], [557, 5.54], [558, 5.56], [559, 5.55], [560, 5.52], [561, 5.52], [562, 5.51], [563, 5.51], [564, 5.50], [565, 5.49], [566, 5.47], [567, 5.46], [568, 5.45], [569, 5.44], [570, 5.47], [571, 5.46], [572, 5.47], [573, 5.49], [574, 5.49], [575, 5.48], [576, 5.49], [577, 5.49], [578, 5.49], [579, 5.48], [580, 5.51], [581, 5.49], [582, 5.49], [583, 5.49], [584, 5.47], [585, 5.45], [586, 5.45], [587, 5.42], [588, 5.42], [589, 5.39], [590, 5.41], [591, 5.42], [592, 5.41], [593, 5.42], [594, 5.44], [595, 5.42], [596, 5.43], [597, 5.43], [598, 5.43], [599, 5.43], [600, 5.44], [601, 5.44], [602, 5.47], [603, 5.47], [604, 5.48], [605, 5.48], [606, 5.51], [607, 5.52], [608, 5.53], [609, 5.52], [610, 5.53], [611, 5.52]]";

1 个答案:

答案 0 :(得分:1)

它不起作用,因为arrayToDataTable()成员函数需要一个Array作为参数,而你传递一个String。

可以使用的一个选项是首先解析作为JSON传递给您的数据。唯一的问题是包裹DateVariable的单引号。单引号不是有效的JSON。如果您能够更改响应,则可以执行以下操作:

google.visualization.arrayToDataTable(JSON.parse(loadGraphData));

否则,如果你需要处理单引号,可以用双引号替换它们,然后解析为JSON:

google.visualization.arrayToDataTable(JSON.parse(loadGraphData.replace(/'/g, '"'));