GraphContainer.tsx 2.6 KB

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