forked from liferay/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request liferay#521 from julien/clay-docs
Add Chart examples to claycss.com
- Loading branch information
Showing
33 changed files
with
1,644 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
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> |
This file contains 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
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> |
This file contains 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
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; |
This file contains 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
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} |
This file contains 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
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
12
packages/clay-charts/src/__tests__/__snapshots__/Geomap.js.snap
This file contains 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
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> | ||
`; |
Oops, something went wrong.