Guest User

Untitled

a guest
Sep 6th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. // ./src/components/App.js
  2. import React from 'react';
  3. import {scaleLinear} from 'd3-scale';
  4. import _ from 'lodash';
  5. import {csv} from 'd3-request';
  6. import StripSeries from './StripSeries';
  7.  
  8. class App extends React.Component {
  9. constructor (props) {
  10. super(props);
  11. this.state = {
  12. data: null,
  13. activeStrip: null,
  14. activePosition: null,
  15. activeValue: null,
  16. };
  17. }
  18.  
  19. componentWillMount() {
  20. csv("./germanCitiesCategories.csv", (error, data) => {
  21. if (error) {
  22. console.log("error loading data file");
  23. }
  24. this.setState({
  25. data: data,
  26. });
  27. })
  28. }
  29.  
  30. handleMouseOver(position, city, value) {
  31. this.setState({
  32. activeStrip: city,
  33. activePosition: position,
  34. activeValue: value,
  35. })
  36. }
  37.  
  38. handleMouseOut() {
  39. this.setState({
  40. activeStrip: null,
  41. activePosition: null,
  42. activeValue: null,
  43. })
  44. }
  45.  
  46. render() {
  47. const {width, height, margin, min, max, dataKey} = this.props;
  48. const scale = scaleLinear()
  49. .domain([min, max])
  50. .range([margin.left, width - margin.right]);
  51.  
  52. return (
  53. <svg
  54. width={width}
  55. height={height}
  56. style={{
  57. backgroundColor: "#E3C44D",
  58. stroke: "black"
  59. }}
  60. >
  61. <text
  62. x={margin.left}
  63. y={margin.top}
  64. style={{
  65. fontFamily: "Verdana, sans-serif",
  66. fontWeight: "normal",
  67. fontSize: 14,
  68. stroke: "none"
  69. }}
  70. >
  71. {dataKey}
  72. </text>
  73. {this.state.activeStrip &&
  74. <text
  75. x={this.state.activePosition}
  76. y={margin.top + 20}
  77. style={{
  78. fontFamily: "Verdana, sans-serif",
  79. fontWeight: "normal",
  80. fontSize: 10,
  81. textAnchor: "middle",
  82. stroke: "none"
  83. }}
  84. >
  85. {`${this.state.activeStrip}: ${this.state.activeValue}`}
  86. </text>
  87. }
  88. <line
  89. x1={margin.left}
  90. y1={height - margin.bottom}
  91. x2={width - margin.right}
  92. y2={height - margin.bottom} />
  93.  
  94.  
  95. <StripSeries
  96. data={this.state.data}
  97. dataKey={dataKey}
  98. scale={scale}
  99. dimensions={this.props}
  100. activeStrip={this.state.activeStrip}
  101. mouseOverHandler={this.handleMouseOver.bind(this)}
  102. mouseOutHandler={this.handleMouseOut.bind(this)} />
  103. </svg>
  104. );
  105. }
  106. }
  107.  
  108. export default App;
Advertisement
Add Comment
Please, Sign In to add comment