The demo below shows a very basic application of the visualization library to create a line chart from a simple HTML table. The HTML table is converted to a DataTable, and the HTML data-* attributes on the div tag are converted to Google Visualization parameters. This particular approach signficantly reduces the logic that has to be implemented using the server-side language.
If you have the data in an HTML table, there are only a couple extra steps you'll need to take. This example assumes that the data for each series is stored in table rows. By default, series are expected to appear in columns as in this example.
Add the JS to the page. If you use the witti CDN, the script tag would look like this:
Add properly formatted HTML table to the page
- Use the <thead> and <th> tags on the first row
- Use the <tbody> tag for the table content
- Optionally use the table's <caption> tag for the chart title
Wrap the table in a div tag with the chart configurations
- Add a <div data-gv-type="LineChart" data-gv-datatablerotate="true"> tag around the table. The data-gv-type attribute is the bare essential for loading a line chart.
- Other Google Visualization properties can be set as HTML5 data tags with the extra prefix for a total prefix of "data-gv-".
- For example, the legend could be moved above the chart using data-gv-legend="top".
Demo (Example Line Chart)
|Year||Company A||Company B||Company C|