GraphContainer.tsx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. width: number;
  21. }
  22. export class GraphContainer extends PureComponent<GraphContainerProps> {
  23. onClickGraphButton = () => {
  24. this.props.toggleGraph(this.props.exploreId, this.props.showingGraph);
  25. };
  26. onChangeTime = (timeRange: TimeRange) => {
  27. this.props.changeTime(this.props.exploreId, timeRange);
  28. };
  29. render() {
  30. const { exploreId, graphResult, loading, showingGraph, showingTable, range, split, width } = this.props;
  31. const graphHeight = showingGraph && showingTable ? 200 : 400;
  32. if (!graphResult) {
  33. return null;
  34. }
  35. return (
  36. <Panel label="Graph" isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
  37. <Graph
  38. data={graphResult}
  39. height={graphHeight}
  40. id={`explore-graph-${exploreId}`}
  41. onChangeTime={this.onChangeTime}
  42. range={range}
  43. split={split}
  44. width={width}
  45. />
  46. </Panel>
  47. );
  48. }
  49. }
  50. function mapStateToProps(state: StoreState, { exploreId }) {
  51. const explore = state.explore;
  52. const { split } = explore;
  53. const item: ExploreItemState = explore[exploreId];
  54. const { graphResult, queryTransactions, range, showingGraph, showingTable } = item;
  55. const loading = queryTransactions.some(qt => qt.resultType === 'Graph' && !qt.done);
  56. return { graphResult, loading, range, showingGraph, showingTable, split };
  57. }
  58. const mapDispatchToProps = {
  59. toggleGraph,
  60. changeTime,
  61. };
  62. export default hot(module)(
  63. connect(
  64. mapStateToProps,
  65. mapDispatchToProps
  66. )(GraphContainer)
  67. );