The Inspiration
Andmap is trying to build new BI product, the tech stacks would be D3 + ReactJS as data visualization frontend, Web API, and Python provides data. As with most new projects, it didn’t take long to figure out the major roadblocks.
D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.
In one of our initial discovery sessions, we realized that our application needs to be able to adapt D3 library in ReactJS way. More than that, we prefer utilizing new feature of React - Hooks. Finally, we build component for each chart and Helpers file for drawing each chart.
Here is sample for map chart:
Andmap is trying to build new BI product, the tech stacks would be D3 + ReactJS as data visualization frontend, Web API, and Python provides data. As with most new projects, it didn’t take long to figure out the major roadblocks.
D3.js is a JavaScript library for manipulating documents based on data. D3 helps you bring data to life using HTML, SVG, and CSS. D3’s emphasis on web standards gives you the full capabilities of modern browsers without tying yourself to a proprietary framework, combining powerful visualization components and a data-driven approach to DOM manipulation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Map data={mapData} tooltipHtml={tooltipHtml} w={w} h={600} /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Ball data={ballData} w={w} h={h / 4} /> | |
<Bar data={barData} w={w} h={400} /> |
using useEffect hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react' | |
import { drawMap } from './helpers.js' | |
import './map.css' | |
const Map = props => { | |
useEffect(() => { | |
drawMap(props) | |
}, [props.data.length]) | |
return ( | |
<> | |
<h2>US Map</h2> | |
<div className='map' /> | |
<div id='tooltip'></div> | |
</> | |
) | |
} | |
export default Map |
helpers.js - to draw charts
How to trigger mouse event in React
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mouseOver = d => { | |
d3.select('#tooltip') | |
.transition() | |
.duration(200) | |
.style('opacity', 0.9) | |
d3.select('#tooltip') | |
.html(tooltipHtml(d.n, data[d.id])) | |
.style('left', d3.event.pageX + 'px') | |
.style('top', d3.event.pageY - 28 + 'px') | |
} | |
const mouseOut = () => { | |
d3.select('#tooltip') | |
.transition() | |
.duration(500) | |
.style('opacity', 0) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
svgContainer | |
.selectAll('path') | |
.data(usStatePaths) | |
.enter() | |
.append('path') | |
.attr('class', 'state') | |
.attr('d', function (d) { | |
return d.d | |
}) | |
.style('fill', function (d) { | |
return data[d.id].color | |
}) | |
.on('mouseover', mouseOver) | |
.on('mouseout', mouseOut) | |
} |
Comments
Post a Comment