Hi Gopalsamy.  You are adding the same columns again and again as you loop
over the rows.  You should add the columns only once, and then loop over the
rows to set the values, like this:

private AbstractDataTable createTable(String xmldata) {
         DataTable data = DataTable.create();

               data.addColumn(ColumnType.STRING, "Year");

               // XML PARSE
               final Document xmlDoc = XMLParser.parse(xmldata);
               final Element root = xmlDoc.getDocumentElement();
               XMLParser.removeWhitespace(xmlDoc);
               final NodeList profiles = root.getElementsByTagName("DATA");

               int rowIndex = 0, columnIndex = -1;

               for (int j = 0; j < noofEmp; j++) {
                 ++columnIndex;
                 final Element emp = (Element)
year.getElementsByTagName("INDIVIDUALS").item(j);
                 final Element emp_name = (Element)
emp.getElementsByTagName("NAME").item(0);

                 data.addColumn(ColumnType.NUMBER,
emp_name.getFirstChild().getNodeValue());
               }
               data.addRows(profiles.getLength());
               columnIndex = -1;
               for (int i = 0; i < profiles.getLength(); i++) {
                       data.addRow();
                       final Element year = (Element) profiles.item(i);

                       int noofEmp =
year.getElementsByTagName("INDIVIDUALS").getLength();

                       data.setValue(rowIndex, ++columnIndex, year

.getAttributeNode("YEAR").getValue());
                       for (int j = 0; j < noofEmp; j++) {
                               ++columnIndex;
                               final Element emp = (Element)
year.getElementsByTagName(
                                               "INDIVIDUALS").item(j);
                               final Element emp_leave = (Element)
emp.getElementsByTagName(
                                               "T_LEAVE").item(0);
                               data.setValue(rowIndex, columnIndex,
Integer.parseInt(emp_leave

.getFirstChild().getNodeValue()));
                       }
                       rowIndex++;
                       columnIndex = -1;
                       }
               return data;
 }

Or, if you know ahead of time that the only employees are Gopal, Shanthi and
Senthi, you can skip the addColumn() loop and just do this:

private AbstractDataTable createTable(String xmldata) {
         DataTable data = DataTable.create();

               data.addColumn(ColumnType.STRING, "Year");

               // XML PARSE
               final Document xmlDoc = XMLParser.parse(xmldata);
               final Element root = xmlDoc.getDocumentElement();
               XMLParser.removeWhitespace(xmlDoc);
               final NodeList profiles = root.getElementsByTagName("DATA");

               int rowIndex = 0, columnIndex = -1;

               data.addRows(profiles.getLength());
               for (int i = 0; i < profiles.getLength(); i++) {
                       data.addRow();
                       final Element year = (Element) profiles.item(i);

                       int noofEmp =
year.getElementsByTagName("INDIVIDUALS").getLength();

                       data.setValue(rowIndex, ++columnIndex, year

.getAttributeNode("YEAR").getValue());
                       for (int j = 0; j < noofEmp; j++) {
                               ++columnIndex;
                               final Element emp = (Element)
year.getElementsByTagName(
                                               "INDIVIDUALS").item(j);
                               final Element emp_leave = (Element)
emp.getElementsByTagName(
                                               "T_LEAVE").item(0);
                               data.setValue(rowIndex, columnIndex,
Integer.parseInt(emp_leave

.getFirstChild().getNodeValue()));
                       }
                       rowIndex++;
                       columnIndex = -1;
                       }
               return data;
 }

On Mon, Aug 30, 2010 at 8:59 AM, Gopalsamy Ponnuraj <
[email protected]> wrote:

> i faced issue of add columns in column chart dynamically.
> My code is
> private AbstractDataTable createTable(String xmldata) {
>          DataTable data = DataTable.create();
>
>                data.addColumn(ColumnType.STRING, "Year");
>                data.addColumn(ColumnType.NUMBER, "Gopal","0");
>                data.addColumn(ColumnType.NUMBER, "Shanthi","1");
>                data.addColumn(ColumnType.NUMBER, "Senthil","2");
>
>                // XML PARSE
>                final Document xmlDoc = XMLParser.parse(xmldata);
>                final Element root = xmlDoc.getDocumentElement();
>                XMLParser.removeWhitespace(xmlDoc);
>                final NodeList profiles = root.getElementsByTagName("DATA");
>
>                int rowIndex = 0, columnIndex = -1;
>
>                //data.addRows(profiles.getLength());
>                for (int i = 0; i < profiles.getLength(); i++) {
>                        data.addRow();
>                        final Element year = (Element) profiles.item(i);
>
>                        int noofEmp =
> year.getElementsByTagName("INDIVIDUALS").getLength();
>
>                        data.setValue(rowIndex, ++columnIndex, year
>
>  .getAttributeNode("YEAR").getValue());
>                        for (int j = 0; j < noofEmp; j++) {
>                                ++columnIndex;
>                                final Element emp = (Element)
> year.getElementsByTagName(
>                                                "INDIVIDUALS").item(j);
>                                final Element emp_leave = (Element)
> emp.getElementsByTagName(
>                                                "T_LEAVE").item(0);
>                                final Element emp_name = (Element)
> emp.getElementsByTagName(
>                                "NAME").item(0);
>
>                                //Window.alert("Num of Record"+noofEmp);
>                        //      data.addColumn(ColumnType.NUMBER, emp_name
>                                //
>  .getFirstChild().getNodeValue());
>
>  data.setColumnLabel(columnIndex,emp_name
>
>  .getFirstChild().getNodeValue());
>
>                                                //Window.alert("Name :
> "+emp_name
>                                //
>  .getFirstChild().getNodeValue());
>
>                                data.setValue(rowIndex, columnIndex,
> Integer.parseInt(emp_leave
>
>  .getFirstChild().getNodeValue()));
>                                //Window.alert("row
> Column"+data.getValueInt(rowIndex,
> columnIndex));
>
>                        }
>                        rowIndex++;
>                        columnIndex = -1;
>                        }
>                //Window.alert("Row Count ,Column
> Count"+data.getNumberOfRows()+"
> "+data.getNumberOfColumns());
>
>
>                return data;
>  }
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Visualization API" group.
> To post to this group, send email to
> [email protected].
> To unsubscribe from this group, send email to
> [email protected]<google-visualization-api%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-visualization-api?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.

Reply via email to