GraphContainer.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import React, { PureComponent } from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import { connect } from 'react-redux';
  4. import { TimeRange, TimeZone, AbsoluteTimeRange } 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. import { getTimeZone } from '../profile/state/selectors';
  11. import { toUtc, dateTime } from '@grafana/ui/src/utils/moment_wrapper';
  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 ? toUtc(absRange.from) : dateTime(absRange.from),
  33. to: timeZone.isUtc ? toUtc(absRange.to) : dateTime(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. return (
  42. <Panel label="Graph" collapsible isOpen={showingGraph} loading={loading} onToggle={this.onClickGraphButton}>
  43. {graphResult && (
  44. <Graph
  45. data={graphResult}
  46. height={graphHeight}
  47. id={`explore-graph-${exploreId}`}
  48. onChangeTime={this.onChangeTime}
  49. range={timeRange}
  50. timeZone={timeZone}
  51. split={split}
  52. width={width}
  53. />
  54. )}
  55. </Panel>
  56. );
  57. }
  58. }
  59. function mapStateToProps(state: StoreState, { exploreId }) {
  60. const explore = state.explore;
  61. const { split } = explore;
  62. const item: ExploreItemState = explore[exploreId];
  63. const { graphResult, graphIsLoading, range, showingGraph, showingTable } = item;
  64. const loading = graphIsLoading;
  65. return { graphResult, loading, range, showingGraph, showingTable, split, timeZone: getTimeZone(state.user) };
  66. }
  67. const mapDispatchToProps = {
  68. toggleGraph,
  69. changeTime,
  70. };
  71. export default hot(module)(
  72. connect(
  73. mapStateToProps,
  74. mapDispatchToProps
  75. )(GraphContainer)
  76. );