GraphContainer.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { RawTimeRange, TimeRange } from '@grafana/ui';
  5. import { ExploreId, ExploreItemState } from 'app/types/explore';
  6. import { StoreState } from 'app/types';
  7. import { toggleGraph } from './state/actions';
  8. import Graph from './Graph';
  9. import Panel from './Panel';
  10. interface GraphContainerProps {
  11. onChangeTime: (range: TimeRange) => void;
  12. exploreId: ExploreId;
  13. graphResult?: any[];
  14. loading: boolean;
  15. range: RawTimeRange;
  16. showingGraph: boolean;
  17. showingTable: boolean;
  18. split: boolean;
  19. toggleGraph: typeof toggleGraph;
  20. }
  21. export class GraphContainer extends PureComponent<GraphContainerProps> {
  22. onClickGraphButton = () => {
  23. this.props.toggleGraph(this.props.exploreId);
  24. };
  25. render() {
  26. const { exploreId, graphResult, loading, onChangeTime, showingGraph, showingTable, range, split } = this.props;
  27. const graphHeight = showingGraph && showingTable ? '200px' : '400px';
  28. return (
  29. <Panel label="Graph" isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
  30. <Graph
  31. data={graphResult}
  32. height={graphHeight}
  33. id={`explore-graph-${exploreId}`}
  34. onChangeTime={onChangeTime}
  35. range={range}
  36. split={split}
  37. />
  38. </Panel>
  39. );
  40. }
  41. }
  42. function mapStateToProps(state: StoreState, { exploreId }) {
  43. const explore = state.explore;
  44. const { split } = explore;
  45. const item: ExploreItemState = explore[exploreId];
  46. const { graphResult, queryTransactions, range, showingGraph, showingTable } = item;
  47. const loading = queryTransactions.some(qt => qt.resultType === 'Graph' && !qt.done);
  48. return { graphResult, loading, range, showingGraph, showingTable, split };
  49. }
  50. const mapDispatchToProps = {
  51. toggleGraph,
  52. };
  53. export default hot(module)(connect(mapStateToProps, mapDispatchToProps)(GraphContainer));