GraphContainer.tsx 2.1 KB

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