GraphContainer.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import moment from 'moment';
  5. import { TimeRange, TimeZone, AbsoluteTimeRange } from '@grafana/ui';
  6. import { ExploreId, ExploreItemState } from 'app/types/explore';
  7. import { StoreState } from 'app/types';
  8. import { toggleGraph, changeTime } from './state/actions';
  9. import Graph from './Graph';
  10. import Panel from './Panel';
  11. import { getTimeZone } from '../profile/state/selectors';
  12. interface GraphContainerProps {
  13. exploreId: ExploreId;
  14. graphResult?: any[];
  15. loading: boolean;
  16. range: TimeRange;
  17. timeZone: TimeZone;
  18. showingGraph: boolean;
  19. showingTable: boolean;
  20. split: boolean;
  21. toggleGraph: typeof toggleGraph;
  22. changeTime: typeof changeTime;
  23. width: number;
  24. }
  25. export class GraphContainer extends PureComponent<GraphContainerProps> {
  26. onClickGraphButton = () => {
  27. this.props.toggleGraph(this.props.exploreId, this.props.showingGraph);
  28. };
  29. onChangeTime = (absRange: AbsoluteTimeRange) => {
  30. const { exploreId, timeZone, changeTime } = this.props;
  31. const range = {
  32. from: timeZone.isUtc ? moment.utc(absRange.from) : moment(absRange.from),
  33. to: timeZone.isUtc ? moment.utc(absRange.to) : moment(absRange.to),
  34. };
  35. changeTime(exploreId, range);
  36. };
  37. render() {
  38. const { exploreId, graphResult, loading, showingGraph, showingTable, range, split, width, timeZone } = this.props;
  39. const graphHeight = showingGraph && showingTable ? 200 : 400;
  40. const timeRange = { from: range.from.valueOf(), to: range.to.valueOf() };
  41. if (!graphResult) {
  42. return null;
  43. }
  44. return (
  45. <Panel label="Graph" isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
  46. <Graph
  47. data={graphResult}
  48. height={graphHeight}
  49. id={`explore-graph-${exploreId}`}
  50. onChangeTime={this.onChangeTime}
  51. range={timeRange}
  52. timeZone={timeZone}
  53. split={split}
  54. width={width}
  55. />
  56. </Panel>
  57. );
  58. }
  59. }
  60. function mapStateToProps(state: StoreState, { exploreId }) {
  61. const explore = state.explore;
  62. const { split } = explore;
  63. const item: ExploreItemState = explore[exploreId];
  64. const { graphResult, queryTransactions, range, showingGraph, showingTable } = item;
  65. const loading = queryTransactions.some(qt => qt.resultType === 'Graph' && !qt.done);
  66. return { graphResult, loading, range, showingGraph, showingTable, split, timeZone: getTimeZone(state.user) };
  67. }
  68. const mapDispatchToProps = {
  69. toggleGraph,
  70. changeTime,
  71. };
  72. export default hot(module)(
  73. connect(
  74. mapStateToProps,
  75. mapDispatchToProps
  76. )(GraphContainer)
  77. );