UNPKG

@hackoregon/component-library

Version:

Official repo for Hack Oregon React component library

108 lines (100 loc) 3.74 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } import React from "react"; import PropTypes from "prop-types"; import { VictoryPie, VictoryLabel, VictoryContainer } from "victory"; import ChartContainer from "../ChartContainer"; import civicTheme from "../VictoryTheme/CivicVictoryTheme"; import SimpleLegend from "../SimpleLegend"; import DataChecker from "../utils/DataChecker"; var PieChart = function PieChart(props) { var title = props.title, subtitle = props.subtitle, colors = props.colors, data = props.data, dataValue = props.dataValue, dataLabel = props.dataLabel, innerRadius = props.innerRadius, loading = props.loading, error = props.error, halfDoughnut = props.halfDoughnut, useLegend = props.useLegend, width = props.width, height = props.height; var startAngle = halfDoughnut ? -90 : 0; var endAngle = halfDoughnut ? 90 : 360; var adjustedHeight = halfDoughnut ? height / 2 : height; var legendLabels = data.map(function (value) { return { name: value[dataLabel] }; }); var legendProps = {}; if (useLegend) { legendProps.labels = function () { return null; }; legendProps.padding = 25; } return React.createElement(ChartContainer, { title: title, subtitle: subtitle, loading: loading, error: error }, React.createElement(DataChecker, { dataAccessors: { dataValue: dataValue }, data: data }, useLegend && React.createElement(SimpleLegend, { className: "legend", legendData: legendLabels, colorScale: colors }), React.createElement(VictoryPie, _extends({ width: width, height: height, data: data, innerRadius: innerRadius, colorScale: colors, theme: civicTheme, animate: { duration: 1000 }, x: dataLabel, y: dataValue, startAngle: startAngle, endAngle: endAngle, labelComponent: React.createElement(VictoryLabel, { style: _objectSpread({}, civicTheme.pieLabel.style) }) }, legendProps, { containerComponent: React.createElement(VictoryContainer, { height: adjustedHeight, width: width }) })))); }; PieChart.defaultProps = { useLegend: false }; PieChart.propTypes = { colors: PropTypes.arrayOf(PropTypes.string), data: PropTypes.arrayOf(PropTypes.shape({})).isRequired, dataLabel: PropTypes.string, dataValue: PropTypes.string, error: PropTypes.string, halfDoughnut: PropTypes.bool, height: PropTypes.number, innerRadius: PropTypes.number, loading: PropTypes.bool, subtitle: PropTypes.string, title: PropTypes.string, useLegend: PropTypes.bool, width: PropTypes.number }; PieChart.defaultProps = { dataLabel: "x", dataValue: "y" }; export default PieChart;