score:1
class Line extends React.Component {
static contextTypes = {
xScale: React.PropTypes.func,
yScale: React.PropTypes.func
};
render() {
let path = d3.svg.line()
.interpolate("linear")
.x(d => this.context.xScale(d.x))
.y(d => this.context.yScale(d.y));
return (
<path d={path(this.props.data)}
stroke="#0077CC"
strokeWidth="3"
fill="none" />
);
}
}
class Chart extends React.Component {
static propTypes = {
width: React.PropTypes.number,
height: React.PropTypes.number,
data: React.PropTypes.shape({
x: React.PropTypes.number.isRequired,
y: React.PropTypes.number.isRequired
}).isRequired
};
static defaultProps = {
width: 400,
height: 200
};
static childContextTypes = {
xScale: React.PropTypes.func,
yScale: React.PropTypes.func
};
getChildContext() {
return {
xScale: this.getXScale(),
yScale: this.getYScale()
}
}
getXScale() {
return d3.scale.linear()
.domain(d3.extent(this.props.data, d => d.x))
.range([0, this.props.width]);
}
getYScale() {
return d3.scale.linear()
.domain(d3.extent(this.props.data, d => d.y))
.range([this.props.height, 0]);
}
render() {
return (
<svg style={{ width: this.props.width, height: this.props.height }}>
<Line data={this.props.data} />
</svg>
);
}
}
Source: stackoverflow.com
Related Query
- Confuse about the overlap and collision between React and D3
- What is the difference between React Native and React?
- What's the difference between "super()" and "super(props)" in React when using es6 classes?
- What's the difference between hydrate() and render() in React 16?
- What is the difference between NextJs and Create React App
- What is the difference between hashHistory and browserHistory in react router?
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- What is the main difference between React Query and Redux?
- What's the difference between a JavaScript function and a React hook?
- React - What is the difference between renderToString and renderToStaticMarkup
- What's the difference between importing React's Fragment from React and React, { Fragment }?
- React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?
- What is the difference between React component instance property and state property?
- What is the difference between React component and React component instance?
- How does React router works and what is the difference between <link> and<Route>
- What is the difference between arrow functions and regular functions inside React functional components (no longer using class components)?
- ES6 React - What are the difference between reference, shallow copy and deep copy and how to compare them?
- What is the difference between useHistory() and props.history in React Route
- what is the difference between getDefaultProps and getInitialState react js
- React Transition Group: What is the difference between the appear, enter, exit events and the enter, active done className suffixes?
- What's the difference between alignItems and alignSelf in React Native?
- What is the difference between accessible, accessibilityLabel and accessibilityHint properties of Text component in react native?
- What’s the difference between React event and DOM event?
- What is the real difference between value and defaultValue in React JS?
- What is the difference between a javascript package, node package, and react package?
- What is the difference between React and Preact diff algorithm in depth
- Differentiate between the previous element and current element from the data of a Flatlist in react native
- What is the difference between a fibre object in React 16 and a React Element?
- React Router - What's the difference between Router and Switch?
- In a React Component, what's the difference between foo(){} and bar = () => {} and when should I use which?
More Query from same tag
- Redux : Cannot pass nested variable to action
- How to maintain state of child components when they are filtered through the parent component?
- Combine two different but pretty similar functions on onChange event
- Pass function into useeffect and return result
- Cypress: How to get response of fourth request from alias?
- Why isn't child's state changing to 'blue'?
- State is not defined
- How to remove redundant/unused dependencies from package.json?
- Express 404 page overlaps the react router
- Cannot read property 'bind' of undefined in react
- Link to google maps doesn't work in react
- Data from API is displaying in the console but not in the DOM, why?
- StencilJS error with React output target - You may need an additional loader to handle the result of these loaders
- React redux thunk how can I get one updated reducer value?
- How remember that a React hooks component is unmounted, so can avoid state changes?
- React - How do you get the top position of a styled component?
- Focus on input is lost after state change
- How to fix the "Type Error: Cannot read property 'map' of undefined?
- Why aren't my stylesheets being included in the Webpack build?
- Return json object to render method - React.js
- How to block UI in React
- How to use locale in material ui datepicker (MuiPickersUtilsProvider)?
- First, the page is rendered, and only then the data comes in, but the old data remains on the page
- React: How to invoke a modal if components are unrelated
- Updating multiple firestone collections
- TypeScript: Access specific key of excluded readonly object literal
- sharing states between two components with useReducer
- How does React Fragment improve Layout?
- GraphQL filtering by data
- CORS Error in a working setup while trying to authenticate using Google+ API in React/Node setup