Mada za sehemu hiiVisualising dataMada 2
- Creating basic plots using libraries
- Creating basic interactive visualisations
Interactive visualisation is a way to display data as charts or graphs that respond to user actions. Unlike static images, interactive charts let you hover over elements to see details, click to filter information, or select specific items to change what is shown. This makes it easier to explore and understand data.
In programming, we use charting libraries — ready-made code packages that handle the difficult work of drawing charts. You provide the data, and the library creates a visual you can interact with.
Building charts from scratch requires complex drawing code. A charting library gives you:
- Pre-built chart types — bar charts, line charts, pie charts, and more
- Built-in interactivity — tooltips, hover effects, and click events
- Customisation options — colours, labels, titles, and legends
- Browser compatibility — works in any modern web browser
Popular libraries include Chart.js, D3.js, and Plotly. Chart.js is especially good for beginners because it is simple to set up and learn.
1. Include the Library
Add the Chart.js library to your HTML file using a CDN link. This makes the library available without downloading anything.
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
2. Prepare Your Data
Organise the data you want to display. For a bar chart, you need labels (categories) and numbers (values).
// Example: Weekly sales at a duka in Arusha
const labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];
const sales = [45000, 62000, 38000, 71000, 55000, 89000, 76000];
3. Create the Chart
Use the library to draw the chart on a canvas element in your HTML.
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Daily Sales (TSh)',
data: sales,
backgroundColor: 'rgba(54, 162, 235, 0.6)',
borderColor: 'rgba(54, 162, 235, 1)',
borderWidth: 1
}]
},
options: {
responsive: true,
plugins: {
tooltip: {
enabled: true
}
}
}
});
4. Add Interactivity
Chart.js provides interactivity automatically:
- Hover tooltips — move your mouse over a bar to see the exact value
- Legend clicks — click legend items to show or hide data
- Responsive design — the chart adjusts to fit the screen
To add custom click events, use the onClick option in the chart options.
Imagine you are helping a mama lishe (vegetable seller) at Mlimani City market track her daily earnings. You want to create an interactive bar chart that shows earnings for each vegetable type.
// Data: Earnings by vegetable (in TSh)
const vegetables = ['Tomatoes', 'Onions', 'Cabbage', 'Carrots', 'Spinach'];
const earnings = [85000, 62000, 45000, 38000, 72000];
const ctx = document.getElementById('marketChart').getContext('2d');
const marketChart = new Chart(ctx, {
type: 'bar',
data: {
labels: vegetables,
datasets: [{
label: 'Earnings (TSh)',
data: earnings,
backgroundColor: [
'rgba(255, 99, 132, 0.6)',
'rgba(54, 162, 235, 0.6)',
'rgba(255, 206, 86, 0.6)',
'rgba(75, 192, 192, 0.6)',
'rgba(153, 102, 255, 0.6)'
],
borderWidth: 1
}]
},
options: {
plugins: {
tooltip: {
callbacks: {
label: function(context) {
return 'Earnings: TSh ' + context.raw.toLocaleString();
}
}
}
},
scales: {
y: {
beginAtZero: true,
title: {
display: true,
text: 'Tanzanian Shillings (TSh)'
}
}
}
}
});
This chart displays each vegetable with a different colour. When you hover over any bar, a tooltip shows the exact earnings in Tanzanian shillings. The chart is fully interactive — you can hover, click the legend to hide specific vegetables, or resize the window and the chart adjusts automatically.
- Tooltips appear when you hover over chart elements, showing extra details
- Legend toggles let you click to show or hide specific data series
- Animation makes charts feel alive when they first load
- Responsiveness ensures charts work on phones, tablets, and computers
These features help users explore data without needing to write extra code.
- Choose the right chart type for your data — use bar charts for comparisons, line charts for trends
- Keep labels clear and easy to read
- Use colours consistently and avoid too many bright colours
- Test your chart on different screen sizes
In Tanzania, small business owners and market traders can use interactive visualisations to track sales, monitor stock levels, and identify which products earn the most money. For example, a shopkeeper at Kariakoo Market in Dar es Salaam could create an interactive chart showing daily sales of different items, hover over each bar to see exact revenue in Tanzanian shillings, and use this information to decide which products to restock more often. This helps them make better business decisions based on real data rather than guesswork.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
The student should be able to:
- Add a charting/visualisation library to the project
- Bind the chart to a dataset
- Add basic interactivity (hover tooltips, click handlers, simple filters)
- Render the chart in a webpage or application
- Verify that the interactive controls behave as expected
Tathmini ya matokeo
Interactive charts and visualisations conform to the required specifications.
Tathmini ya maarifa
Underpinning knowledge of
Methods used: The student should explain methods related to creating basic interactive visualisations
Principles: The student should state the principles of creating basic interactive visualisations
Theories: The student should explain theories related to creating basic interactive visualisations
Circumstantial knowledge: The student should explain detailed knowledge related to creating basic interactive visualisations
Swali
What is interactive visualisation?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado