Skip to content

Commit

Permalink
Merge pull request liferay#521 from julien/clay-docs
Browse files Browse the repository at this point in the history
Add Chart examples to claycss.com
  • Loading branch information
jbalsas authored and Julien Castelain committed Feb 8, 2018
2 parents 0a77c12 + 9bfc52c commit 82c43f9
Show file tree
Hide file tree
Showing 33 changed files with 1,644 additions and 2 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"scripts": {
"a11y": "metal-a11y --packages ./packages -c demos/a11y.html",
"build": "lerna run build",
"compile": "lerna run compile",
"checkFormat": "npm run prettier -- --list-different",
"coveralls": "node node_modules/coveralls/bin/coveralls.js < coverage/lcov.info",
"format": "npm run prettier -- --write",
Expand All @@ -12,7 +13,10 @@
"mcritic": "mcritic packages/ --ignore '**/{browserslist-config-clay-components,claycss.com,generator-metal-clay,node_modules}/**'",
"prettier": "prettier-eslint packages/clay-*/src/*.js packages/clay-*/src/**/*.js",
"start": "http-server . -p 4000",
"test": "npm run build && npm run jest && npm run a11y"
"test": "npm run build && npm run jest && npm run a11y",
"electric": "npm run compile && cd packages/claycss.com && el",
"web": "npm run electric -- run",
"deploy": "npm run electric -- deploy"
},
"devDependencies": {
"babel-preset-metal": "^4.1.0",
Expand Down
1 change: 1 addition & 0 deletions packages/clay-charts/demos/data/world-low-res.geo.json

Large diffs are not rendered by default.

45 changes: 45 additions & 0 deletions packages/clay-charts/demos/geomap-jsx.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo: Metal Geomap JSX</title>

<style type="text/css">
.flex{
display: flex;
align-items: center;
flex-direction: column;
}
.map {
width: 800px;
height: 600px;
}
</style>

<script src="../build/globals/clay-charts-jsx.js"></script>
</head>
<body>
<h3>Metal Geomap (JSX)</h3>

<div class="flex">
<div class="map" id="map"></div>
</div>

<script type="text/javascript">
var geomap = new metal.Geomap({
color: {
range: {
min: 'aliceblue',
max: 'darkblue',
},
selected: '#0000dd',
value: 'pop_est',
},
data: 'data/world-low-res.geo.json',
}, '#map');


</script>
</body>
</html>
45 changes: 45 additions & 0 deletions packages/clay-charts/demos/geomap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo: Metal Geomap</title>

<style type="text/css">
.flex{
display: flex;
align-items: center;
flex-direction: column;
}
.map {
width: 800px;
height: 600px;
}
</style>

<script src="../build/globals/clay-charts.js"></script>
</head>
<body>
<h3>Metal Geomap</h3>

<div class="flex">
<div class="map" id="map"></div>
</div>

<script type="text/javascript">
var geomap = new metal.Geomap({
color: {
range: {
min: 'hotpink',
max: '#cc0000',
},
selected: '#ff3300',
value: 'gdp_md_est',
},
data: 'data/world-low-res.geo.json',
}, '#map');


</script>
</body>
</html>
229 changes: 229 additions & 0 deletions packages/clay-charts/src/Geomap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
import Component from 'metal-component';
import Soy from 'metal-soy';
import {Config} from 'metal-state';
import * as d3 from 'd3';

import templates from './Geomap.soy.js';

/**
* Geomap component
*/
class Geomap extends Component {
/**
* @inheritDoc
*/
attached() {
const w =
typeof this.width_ === 'string' ? this.width_ : `${this.width_}px`;
const h =
typeof this.height_ === 'string'
? this.height_
: `${this.height_}px`;

this.svg = d3
.select(this.element)
.append('svg')
.attr('width', w)
.attr('height', h);

this.handleClickHandler_ = this.handleClick_.bind(this);

this.rect = this.svg
.append('rect')
.attr('fill', 'rgba(1, 1, 1, 0)')
.attr('width', w)
.attr('height', h)
.on('click', this.handleClickHandler_);

const bounds = this.svg.node().getBoundingClientRect();

this.svgGroup = this.svg.append('g');
this.mapLayer = this.svgGroup.append('g');
this.projection = d3
.geoMercator()
.scale(100)
.translate([bounds.width / 2, bounds.height / 2]);

this.path = d3.geoPath().projection(this.projection);
this.selected_ = null;

d3.json(this.data, this.onDataLoad_.bind(this));
}

/**
* Fill function
* @param {Object} d
* @return {Number}
* @protected
*/
fillFn_(d) {
const value = d && d.properties ? d.properties[this.color.value] : 0;
return this.colorScale(value);
}

/**
* Click handler
* @param {Object} d
* @protected
*/
handleClick_(d) {
if (d && this.selected_ !== d) {
this.selected_ = d;
} else {
this.selected_ = null;
}

// Highlight the clicked province
this.mapLayer
.selectAll('path')
.style(
'fill',
d =>
this.selected_ && d === this.selected_
? this.color.selected
: this.fillFn_.bind(this)(d)
);
}

/**
* Mouse over handler
* @param {Object} feature
* @param {Number} idx
* @param {Array} selection
* @protected
*/
handleMouseOver_(feature, idx, selection) {
const node = selection[idx];
d3.select(node).style('fill', this.color.selected);
}

/**
* Mouse over handler
* @param {Object} feature
* @param {Number} idx
* @param {Array} selection
* @protected
*/
handleMouseOut_(feature, idx, selection) {
const node = selection[idx];
d3.select(node).style('fill', this.fillFn_.bind(this));
}

/**
* Data load handler
* @param {Error} err
* @param {Object} mapData
* @protected
*/
onDataLoad_(err, mapData) {
if (err) {
throw err;
}
const features = mapData.features;

// Calculate domain based on values received
const values = features.map(f => f.properties[this.color.value]);

this.domainMin_ = Math.min.apply(null, values);
this.domainMax_ = Math.max.apply(null, values);

this.colorScale = d3
.scaleLinear()
.domain([this.domainMin_, this.domainMax_])
.range([this.color.range.min, this.color.range.max]);

this.mapLayer
.selectAll('path')
.data(features)
.enter()
.append('path')
.attr('d', this.path)
.attr('vector-effect', 'non-scaling-stroke')
.attr('fill', this.fillFn_.bind(this))
.on('click', this.handleClickHandler_)
.on('mouseout', this.handleMouseOut_.bind(this))
.on('mouseover', this.handleMouseOver_.bind(this));
}
}

Soy.register(Geomap, templates);

/**
* GeoMap state definition.
* @type {!Object}
* @static
*/
Geomap.STATE = {
/**
* Color configuration.
* @instance
* @memberof ChartBase
* @type {?Object}
* @default undefined
*/
color: Config.shapeOf({
range: Config.shapeOf({
max: Config.string().required(),
min: Config.string().required(),
}),
selected: Config.string(),
value: Config.string().required(),
}).value({
range: {
min: '#b1d4ff',
max: '#0065e4',
},
selected: '#4b9bff',
value: 'pop_est',
}),

/**
* Minimum value for domain
* @instance
* @memberof Geomap
* @type {Number}
*/
domainMin_: Config.number().internal(),

/**
* Maximum value for domain
* @instance
* @memberOf Geomap
* @type {Number}
*/
domainMax_: Config.number().internal(),

/**
* Path to the geo-json data
* @instance
* @memberof Geomap
* @type {?String|undefined}
* @default undefined
*/
data: Config.string().required(),

/**
* Height of the map
* @instance
* @memberof Geomap
* @type {?Number}
* @default 480
*/
height_: Config.oneOfType([Config.string(), Config.number()])
.value('100%')
.internal(),

/**
* Width of the map
* @instance
* @memberof Geomap
* @type {?Number}
* @default 640
*/
width_: Config.oneOfType([Config.string(), Config.number()])
.value('100%')
.internal(),
};

export {Geomap};
export default Geomap;
10 changes: 10 additions & 0 deletions packages/clay-charts/src/Geomap.soy
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{namespace ClayGeomap}

/**
* This renders the main element
*/
{template .render}
{@param? height_: string}
{@param? width_: string}
<div style="width:{$width_};height:{$height_}"></div>
{/template}
24 changes: 24 additions & 0 deletions packages/clay-charts/src/__tests__/Geomap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Geomap from '../Geomap';

/**
* Creates a Geomap
* @param {Object} config
* @return {Geomap}
*/
function createMap(config = {}) {
return new Geomap(config);
}

describe('Geomap', () => {
it('should throw an error if required configuration is invalid', () => {
expect(createMap).toThrow();
});

it('should not throw an error', () => {
const geomap = createMap({
data: '../../demos/data/world-low-res.geo.json',
});

expect(geomap).toMatchSnapshot();
});
});
12 changes: 12 additions & 0 deletions packages/clay-charts/src/__tests__/__snapshots__/Geomap.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Geomap should not throw an error 1`] = `
<div style="width: 100%; height: 100%;">
<svg width="100%" height="100%">
<rect fill="rgba(1, 1, 1, 0)" width="100%" height="100%"></rect>
<g>
<g></g>
</g>
</svg>
</div>
`;
Loading

0 comments on commit 82c43f9

Please sign in to comment.