我正在嘗試製作一個動態餅圖,它會根據滑塊的變化進行更改。 我已經設置了一切但是我不能在它中引入狀態,因爲我是新的反應。我只是想當有人滑動滑塊時,餅圖也會根據滑塊的值而改變。誰能幫我?如何更改制作動態餅圖的狀態?
<!DOCTYPE html>
<html>
\t <head>
\t \t <title>React Example without Babel </title>
\t \t <script src="https://npmcdn.com/[email protected]/dist/react.min.js"></script>
\t \t <script src="https://npmcdn.com/[email protected]/dist/react-dom.min.js"></script>
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0-alpha1/JSXTransformer.js"></script>
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-with-addons.js"></script>
\t \t <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/1.0.2/Chart.min.js"></script>
\t \t <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">
\t \t <style>
\t \t \t .pie{
\t \t \t \t fill:red;
\t \t \t }
\t \t </style>
\t </head>
\t <body>
\t \t <div class="container">
\t \t \t <div id="app"></div>
\t \t \t <div class="col-lg-5" id="slider"></div>
\t \t \t <div class="col-lg-6" id="pie_chart"></div>
\t \t </div>
\t \t <script type="text/jsx">
\t \t \t
\t \t \t class Header extends React.Component{
\t \t \t \t render(){
\t \t \t \t \t return (<div><h1 className='page-header'>{this.props.text}</h1></div>);
\t \t \t \t }
\t \t \t }
\t \t \t ReactDOM.render(<Header text='Dynamic Pie Chart'/>,document.getElementById('app'));
\t \t \t class Slider extends React.Component{
\t \t \t \t render(){
\t \t \t \t \t return (<div><input type='range' min='1' max='100' /></div>);
\t \t \t \t }
\t \t \t }
\t \t \t ReactDOM.render(<Slider />,document.getElementById('slider'));
\t \t \t var PropTypes = React.PropTypes;
\t \t \t var PieChart = React.createClass({
\t \t \t displayName: 'PieChart',
\t \t \t propTypes: {
\t \t \t \t className: PropTypes.string,
\t \t \t \t size: PropTypes.number,
\t \t \t \t slices: PropTypes.arrayOf(PropTypes.shape({
\t \t \t \t color: PropTypes.string.isRequired, // hex color
\t \t \t \t value: PropTypes.number.isRequired })).isRequired },
\t \t \t \t getDefaultProps: function getDefaultProps() {
\t \t \t \t \t return {
\t \t \t \t \t \t size: 200 };
\t \t \t \t \t },
\t \t \t \t
_renderPaths: function _renderPaths() {
var radCircumference = Math.PI * 2;
var center = this.props.size/2;
var radius = center - 1; // padding to prevent clipping
var total = this.props.slices.reduce(function (totalValue, slice) {
return totalValue + slice.value;
}, 0);
var radSegment = 0;
var lastX = radius;
var lastY = 0;
return this.props.slices.map(function (slice, index) {
var color = slice.color;
var value = slice.value;
// Should we just draw a circle?
if (value === total) {
return React.createElement('circle', {
r: radius,
cx: radius,
cy: radius,
fill: color,
key: index
});
}
if (value === 0) {
return;
}
var valuePercentage = value/total;
// Should the arc go the long way round?
var longArc = valuePercentage <= 0.5 ? 0 : 1;
radSegment += valuePercentage * radCircumference;
var nextX = Math.cos(radSegment) * radius;
var nextY = Math.sin(radSegment) * radius;
// d is a string that describes the path of the slice.
// The weirdly placed minus signs [eg, (-(lastY))] are due to the fact
// that our calculations are for a graph with positive Y values going up,
// but on the screen positive Y values go down.
var d = ['M ' + center + ',' + center, 'l ' + lastX + ',' + -lastY, 'a' + radius + ',' + radius, '0', '' + longArc + ',0', '' + (nextX - lastX) + ',' + -(nextY - lastY), 'z'].join(' ');
lastX = nextX;
lastY = nextY;
return React.createElement('path', { d: d, fill: color, key: index });
});
},
/**
* @return {Object}
*/
render: function render() {
var size = this.props.size;
var center = size/2;
return React.createElement(
'svg',
{ viewBox: '0 0 ' + size + ' ' + size },
React.createElement(
'g',
{ transform: 'rotate(-90 ' + center + ' ' + center + ')' },
this._renderPaths()
)
);
}
});
var slices = [
{ color: '#468966', value: 180 },
{ color: '#FFF0A5', value: 180 },
];
ReactDOM.render(<PieChart slices={slices} />, document.getElementById('pie_chart'));
\t \t
\t \t \t
\t \t </script>
\t </body>
</html>
但即使滑塊走到左側的末端,綠色弧的一些部分是可見的 – shanu
我已經做到了,感謝您的幫助 – shanu
嘿suyesh我需要一些更多的幫助,如果你有空? – shanu