当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Adding Y values of duplicate X values together in XYSeries

问题描述:

public XYSeriesCollection CreateDataset(int startColumn, int endColumn, int startRow, int endRow) {

XYSeriesCollection dataset = new XYSeriesCollection();

XYSeries series = new XYSeries("Test");

for (int j = startRow; j < endRow; j++) {

String curVal = m_Data[j][endColumn];

if (isInt(curVal)) {

XYDataItem item = series.addOrUpdate(Integer.parseInt(m_Data[j][startColumn]), Integer.parseInt(curVal));

if (item != null) {

int temp = item.getY().intValue() + Integer.parseInt(curVal);

int temp1 = series.indexOf(Integer.parseInt(m_Data[j][startColumn]));

/*Integer temp2 = new Integer(Integer.parseInt(m_Data[j][startColumn]));

series.update((Number) temp2, temp);*/

series.updateByIndex(temp1, temp);

}

} /*else if (isDouble(curVal)) {

series.add(Integer.parseInt(m_Data[j][startColumn]), Double.parseDouble(curVal));

} else {

series.add(Integer.parseInt(m_Data[j][startColumn]), 0);

}*/

}

dataset.addSeries(series);

return dataset;

}

Contained in m_Data is a CSV file that has been formatted into String[Rows][Columns]. What I am trying to do is make a dataset used for a XYBarchart where the startColumn indicates which column, and the for loop is used to go through the rows of that column to take the values for the X-Axis (keys?). The values for Y are retrieved from the endColumn on the same row.

In the m_Data array however are duplicate values, so when using a column with duplicate values as the X-Axis, I need to add the corresponding Y values together. However the method I have implemented does not seem to work, and instead just adds the first encountered value to the series.

网友答案:

You will need to subclass XYSeries and replace either XYDataItem addOrUpdate(Number x, Number y) or public void add(XYDataItem item, boolean notify) so that rather the additional values are added rather then overwriting the existing Y value.

Alternatively iterate through you dataset twice once to aggregate duplicates and then to add it to an XYSeries

分享给朋友:
您可能感兴趣的文章:
随机阅读: