# Charts in Game UI

> Charts within a game UI are essential for displaying various statistics, such as resource changes over time, leadership standings, and player attributes like accuracy, kills, deaths, and wins. Gameface supports d3, a JavaScript library for creating and manipulating SVG elements for data visualization. You can build and customize various charts using its low-level API.

> Documentation index: https://coherent-labs.com/llms.txt lists every page of this documentation as Markdown; the full text is in https://coherent-labs.com/llms-full.txt.

Source: https://coherent-labs.com/blog/uitutorials/charts/

## Charts in Game UI

ui tutorials

6/6/2024

Veneta Kochovska, Kaloyan Geshev

1. Showcase overview
   1. Source location
2. Sample specifics
   1. Importing the d3.js library
   2. d3.js charts
   3. Chart data
   4. Gameface components
   5. Updating the range of each chart

Charts within a game UI are essential for displaying various statistics, such as resource changes over time, leadership standings, and player attributes like accuracy, kills, deaths, and wins.

Gameface supports [d3 ](https://d3js.org/), a JavaScript library for creating and manipulating SVG elements for data visualization. You can build and customize various charts using its low-level [API ](https://d3js.org/api).

**Currently Gameface has full support for version 4 and below of the D3JS library. Newer versions have only partial support so the examples shown in this tutorial may not work with it.**

## Showcase overview

In this example, we created a window that presents data through various charts made with d3.js. We have implemented a line chart, bar chart, and pie chart displaying data about gold per year. Additionally, we have an area chart that shows all resources over the period between 0 and 1000 years.

Each chart includes a range slider below it, allowing you to adjust the displayed data period and showing you how to manipulate the shown data in real time.

### Source location

You can review the complete sample source within the `${Gameface package}/Samples/uiresources/UITutorials/StatisticsScreen` directory.

## Sample specifics

### Importing the d3.js library

The easiest way to import d3 is by using their provided CDNs (Content Delivery Network).

```html
<script src="https://d3js.org/d3.v4.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
```

### d3.js charts

We used some pre-made examples from the [d3-graph-gallery ](https://d3-graph-gallery.com/index.html)and adapted them for our demo needs. If you’re not familiar with the `d3.js` library, please refer to the documentation on their website before customizing the charts.

- [Line chart](https://d3-graph-gallery.com/graph/line_change_data.html)
- [Bar chart](https://d3-graph-gallery.com/graph/barplot_button_data_hard.html)
- [Area chart](https://d3-graph-gallery.com/graph/stackedarea_template.html)
- [Pie chart](https://d3-graph-gallery.com/graph/pie_changeData.html)

For each chart, we created a separate file that creates the chart and has an update method to refresh the chart based on new data and range. You can check `src/areaChart.js`, `src/barChart.js`, `src/lineChart.js`, and `src/pieChart.js`.

### Chart data

Random data generation is handled in the `src/generateData.js` file. The `generateChartData` method regenerates the data based on the year range and specified labels.

```javascript
function generateChartData(labels, yearsRange) {
    const chartData = [];
    const years = generateYearsFromRange(yearsRange);

    for (let i = 0; i < years.length; i++) {
        const yearData = { year: years[i] };

        for (const label of labels) {
            const el = generatedData[label].find((el) => el.year === yearData.year);
            yearData[label] = el ? el.value : '0';
        }

        chartData.push(yearData);
    }

    return chartData;
}
```

For example, to get the data for `gold` from year `500` to `1000`, you would call the method like this:

```javascript
generateChartData(['gold'], [500, 1000]);
```

### Gameface components

In the sample, we also used various Gameface components such as [`gameface-tabs` ](https://coherentlabs.github.io/GameUIComponents/en/components/tabs/), [`gameface-grid` ](https://coherentlabs.github.io/GameUIComponents/en/components/grid/), [`gameface-scrollable-container` ](https://coherentlabs.github.io/GameUIComponents/en/components/scrollable-container/), and [`gameface-rangeslider` ](https://coherentlabs.github.io/GameUIComponents/en/components/rangeslider/)to create a more structured and appealing UI.

### Updating the range of each chart

We used the `gameface-rangeslider` component with the `two-handles` configuration to define the period from and to a specific year.

We listen for the `sliderupdate` event. When its value changes, the corresponding chart data is regenerated, and the chart’s `.update` method is called with the new data for the specified range.

## On this page
