Analysis.js 15 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
2
import { connect } from 'dva';
niko's avatar
niko committed
3 4 5 6 7 8 9 10 11 12 13 14 15
import {
  Row,
  Col,
  Icon,
  Card,
  Tabs,
  Table,
  Radio,
  DatePicker,
  Tooltip,
  Menu,
  Dropdown,
} from 'antd';
偏右's avatar
偏右 committed
16
import {
niko's avatar
niko committed
17 18 19 20 21 22 23 24
  ChartCard,
  MiniArea,
  MiniBar,
  MiniProgress,
  Field,
  Bar,
  Pie,
  TimelineChart,
niko's avatar
niko committed
25 26 27
} from 'components/Charts';
import Trend from 'components/Trend';
import NumberInfo from 'components/NumberInfo';
jim's avatar
jim committed
28 29
import numeral from 'numeral';
import GridContent from '../../layouts/GridContent';
jim's avatar
jim committed
30
import Yuan from '../../utils/Yuan';
31 32 33 34
import { getTimeDistance } from '../../utils/utils';

import styles from './Analysis.less';

afc163's avatar
afc163 committed
35
const { TabPane } = Tabs;
36 37 38 39 40 41 42 43 44 45
const { RangePicker } = DatePicker;

const rankingListData = [];
for (let i = 0; i < 7; i += 1) {
  rankingListData.push({
    title: `工专路 ${i} 号店`,
    total: 323234,
  });
}

Andreas Cederström's avatar
Andreas Cederström committed
46 47 48
@connect(({ chart, loading }) => ({
  chart,
  loading: loading.effects['chart/fetch'],
49 50 51 52 53
}))
export default class Analysis extends Component {
  state = {
    salesType: 'all',
    currentTabKey: '',
afc163's avatar
afc163 committed
54
    rangePickerValue: getTimeDistance('year'),
niko's avatar
niko committed
55
  };
56 57

  componentDidMount() {
58 59
    const { dispatch } = this.props;
    dispatch({
60
      type: 'chart/fetch',
61
    });
62 63 64 65 66 67 68 69 70
  }

  componentWillUnmount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'chart/clear',
    });
  }

jim's avatar
jim committed
71
  handleChangeSalesType = e => {
72 73 74
    this.setState({
      salesType: e.target.value,
    });
niko's avatar
niko committed
75
  };
76

jim's avatar
jim committed
77
  handleTabChange = key => {
78 79 80
    this.setState({
      currentTabKey: key,
    });
niko's avatar
niko committed
81
  };
82

jim's avatar
jim committed
83
  handleRangePickerChange = rangePickerValue => {
84
    const { dispatch } = this.props;
85 86 87
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
88

89
    dispatch({
nikogu's avatar
nikogu committed
90 91
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
92
  };
93

jim's avatar
jim committed
94
  selectDate = type => {
95
    const { dispatch } = this.props;
96 97 98 99
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

100
    dispatch({
101 102
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
103
  };
104

afc163's avatar
afc163 committed
105 106 107 108 109 110
  isActive(type) {
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
      return;
    }
niko's avatar
niko committed
111 112 113 114
    if (
      rangePickerValue[0].isSame(value[0], 'day') &&
      rangePickerValue[1].isSame(value[1], 'day')
    ) {
afc163's avatar
afc163 committed
115 116 117 118
      return styles.currentDate;
    }
  }

119
  render() {
120
    const { rangePickerValue, salesType, currentTabKey } = this.state;
Andreas Cederström's avatar
Andreas Cederström committed
121
    const { chart, loading } = this.props;
122 123
    const {
      visitData,
afc163's avatar
afc163 committed
124
      visitData2,
125 126 127 128 129 130 131
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
afc163's avatar
afc163 committed
132
    } = chart;
133

niko's avatar
niko committed
134 135 136
    const salesPieData =
      salesType === 'all'
        ? salesTypeData
jim's avatar
jim committed
137 138 139
        : salesType === 'online'
          ? salesTypeDataOnline
          : salesTypeDataOffline;
140

afc163's avatar
afc163 committed
141 142 143 144 145 146 147
    const menu = (
      <Menu>
        <Menu.Item>操作一</Menu.Item>
        <Menu.Item>操作二</Menu.Item>
      </Menu>
    );

148 149
    const iconGroup = (
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
150 151 152
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
153 154 155
      </span>
    );

afc163's avatar
afc163 committed
156 157 158
    const salesExtra = (
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
afc163's avatar
afc163 committed
159 160 161 162 163 164 165 166 167 168 169 170
          <a className={this.isActive('today')} onClick={() => this.selectDate('today')}>
            今日
          </a>
          <a className={this.isActive('week')} onClick={() => this.selectDate('week')}>
            本周
          </a>
          <a className={this.isActive('month')} onClick={() => this.selectDate('month')}>
            本月
          </a>
          <a className={this.isActive('year')} onClick={() => this.selectDate('year')}>
            全年
          </a>
afc163's avatar
afc163 committed
171 172 173 174 175 176
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={this.handleRangePickerChange}
          style={{ width: 256 }}
        />
177
      </div>
afc163's avatar
afc163 committed
178
    );
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

    const columns = [
      {
        title: '排名',
        dataIndex: 'index',
        key: 'index',
      },
      {
        title: '搜索关键词',
        dataIndex: 'keyword',
        key: 'keyword',
        render: text => <a href="/">{text}</a>,
      },
      {
        title: '用户数',
        dataIndex: 'count',
        key: 'count',
        sorter: (a, b) => a.count - b.count,
afc163's avatar
afc163 committed
197
        className: styles.alignRight,
198 199 200 201 202 203 204
      },
      {
        title: '周涨幅',
        dataIndex: 'range',
        key: 'range',
        sorter: (a, b) => a.range - b.range,
        render: (text, record) => (
偏右's avatar
偏右 committed
205
          <Trend flag={record.status === 1 ? 'down' : 'up'}>
206 207 208 209
            <span style={{ marginRight: 4 }}>
              {text}
              %
            </span>
偏右's avatar
偏右 committed
210
          </Trend>
211
        ),
afc163's avatar
afc163 committed
212
        align: 'right',
213 214 215
      },
    ];

nikogu's avatar
nikogu committed
216 217
    const activeKey = currentTabKey || (offlineData[0] && offlineData[0].name);

218
    const CustomTab = ({ data, currentTabKey: currentKey }) => (
niko's avatar
niko committed
219
      <Row gutter={8} style={{ width: 138, margin: '8px 0' }}>
220 221 222 223
        <Col span={12}>
          <NumberInfo
            title={data.name}
            subTitle="转化率"
偏右's avatar
偏右 committed
224 225
            gap={2}
            total={`${data.cvr * 100}%`}
niko's avatar
niko committed
226
            theme={currentKey !== data.name && 'light'}
227 228 229 230
          />
        </Col>
        <Col span={12} style={{ paddingTop: 36 }}>
          <Pie
nikogu's avatar
nikogu committed
231
            animate={false}
niko's avatar
niko committed
232
            color={currentKey !== data.name && '#BDE4FF'}
233 234 235 236 237 238 239 240 241 242 243 244 245
            inner={0.55}
            tooltip={false}
            margin={[0, 0, 0, 0]}
            percent={data.cvr * 100}
            height={64}
          />
        </Col>
      </Row>
    );

    const topColResponsiveProps = {
      xs: 24,
      sm: 12,
niko's avatar
niko committed
246 247 248
      md: 12,
      lg: 12,
      xl: 6,
249 250 251 252
      style: { marginBottom: 24 },
    };

    return (
jim's avatar
jim committed
253
      <GridContent>
254 255 256 257
        <Row gutter={24}>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
niko's avatar
niko committed
258
              title="总销售额"
niko's avatar
niko committed
259 260 261 262 263
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
264
              loading={loading}
jim's avatar
jim committed
265
              total={() => <Yuan>126560</Yuan>}
afc163's avatar
afc163 committed
266
              footer={<Field label="日均销售额" value={`¥${numeral(12423).format('0,0')}`} />}
267 268
              contentHeight={46}
            >
afc163's avatar
afc163 committed
269
              <Trend flag="up" style={{ marginRight: 16 }}>
270 271
                周同比
                <span className={styles.trendText}>12%</span>
afc163's avatar
afc163 committed
272 273
              </Trend>
              <Trend flag="down">
274 275
                日环比
                <span className={styles.trendText}>11%</span>
afc163's avatar
afc163 committed
276
              </Trend>
277 278 279 280 281
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
282
              loading={loading}
283
              title="访问量"
niko's avatar
niko committed
284 285 286 287 288
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
289 290 291 292
              total={numeral(8846).format('0,0')}
              footer={<Field label="日访问量" value={numeral(1234).format('0,0')} />}
              contentHeight={46}
            >
niko's avatar
niko committed
293
              <MiniArea color="#975FE4" data={visitData} />
294 295 296 297 298
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
299
              loading={loading}
300
              title="支付笔数"
niko's avatar
niko committed
301 302 303 304 305
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
306 307 308 309
              total={numeral(6560).format('0,0')}
              footer={<Field label="转化率" value="60%" />}
              contentHeight={46}
            >
niko's avatar
niko committed
310
              <MiniBar data={visitData} />
311 312 313 314
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
315
              loading={loading}
316
              bordered={false}
niko's avatar
niko committed
317
              title="运营活动效果"
niko's avatar
niko committed
318 319 320 321 322
              action={
                <Tooltip title="指标说明">
                  <Icon type="info-circle-o" />
                </Tooltip>
              }
323
              total="78%"
afc163's avatar
afc163 committed
324
              footer={
afc163's avatar
afc163 committed
325
                <div style={{ whiteSpace: 'nowrap', overflow: 'hidden' }}>
afc163's avatar
afc163 committed
326
                  <Trend flag="up" style={{ marginRight: 16 }}>
327 328
                    周同比
                    <span className={styles.trendText}>12%</span>
afc163's avatar
afc163 committed
329 330
                  </Trend>
                  <Trend flag="down">
331 332
                    日环比
                    <span className={styles.trendText}>11%</span>
afc163's avatar
afc163 committed
333
                  </Trend>
偏右's avatar
偏右 committed
334
                </div>
afc163's avatar
afc163 committed
335
              }
336 337
              contentHeight={46}
            >
niko's avatar
niko committed
338
              <MiniProgress percent={78} strokeWidth={8} target={80} color="#13C2C2" />
339 340 341 342
            </ChartCard>
          </Col>
        </Row>

niko's avatar
niko committed
343
        <Card loading={loading} bordered={false} bodyStyle={{ padding: 0 }}>
344
          <div className={styles.salesCard}>
afc163's avatar
afc163 committed
345
            <Tabs tabBarExtraContent={salesExtra} size="large" tabBarStyle={{ marginBottom: 24 }}>
346
              <TabPane tab="销售额" key="sales">
niko's avatar
niko committed
347
                <Row>
nikogu's avatar
nikogu committed
348
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
349
                    <div className={styles.salesBar}>
nikogu's avatar
nikogu committed
350
                      <Bar height={295} title="销售额趋势" data={salesData} />
351
                    </div>
352
                  </Col>
nikogu's avatar
nikogu committed
353
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
354 355 356
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店销售额排名</h4>
                      <ul className={styles.rankingList}>
niko's avatar
niko committed
357 358 359 360 361 362 363
                        {rankingListData.map((item, i) => (
                          <li key={item.title}>
                            <span className={i < 3 ? styles.active : ''}>{i + 1}</span>
                            <span>{item.title}</span>
                            <span>{numeral(item.total).format('0,0')}</span>
                          </li>
                        ))}
364 365
                      </ul>
                    </div>
366 367 368
                  </Col>
                </Row>
              </TabPane>
369
              <TabPane tab="访问量" key="views">
陈帅's avatar
陈帅 committed
370
                <Row>
371 372
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesBar}>
niko's avatar
niko committed
373
                      <Bar height={292} title="访问量趋势" data={salesData} />
374 375 376 377 378 379
                    </div>
                  </Col>
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店访问量排名</h4>
                      <ul className={styles.rankingList}>
niko's avatar
niko committed
380 381
                        {rankingListData.map((item, i) => (
                          <li key={item.title}>
382
                            <span className={i < 3 ? styles.active : ''}>{i + 1}</span>
niko's avatar
niko committed
383 384 385 386
                            <span>{item.title}</span>
                            <span>{numeral(item.total).format('0,0')}</span>
                          </li>
                        ))}
387 388 389 390
                      </ul>
                    </div>
                  </Col>
                </Row>
391 392 393 394 395 396
              </TabPane>
            </Tabs>
          </div>
        </Card>

        <Row gutter={24}>
397
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
398
            <Card
399
              loading={loading}
400 401 402 403 404 405 406 407
              bordered={false}
              title="线上热门搜索"
              extra={iconGroup}
              style={{ marginTop: 24 }}
            >
              <Row gutter={68}>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
afc163's avatar
afc163 committed
408 409 410 411 412 413 414 415
                    subTitle={
                      <span>
                        搜索用户数
                        <Tooltip title="指标文案">
                          <Icon style={{ marginLeft: 8 }} type="info-circle-o" />
                        </Tooltip>
                      </span>
                    }
偏右's avatar
偏右 committed
416
                    gap={8}
417 418 419 420
                    total={numeral(12321).format('0,0')}
                    status="up"
                    subTotal={17.1}
                  />
niko's avatar
niko committed
421
                  <MiniArea line height={45} data={visitData2} />
422 423 424 425 426 427 428
                </Col>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
                    subTitle="人均搜索次数"
                    total={2.7}
                    status="down"
                    subTotal={26.2}
偏右's avatar
偏右 committed
429
                    gap={8}
430
                  />
niko's avatar
niko committed
431
                  <MiniArea line height={45} data={visitData2} />
432 433 434 435
                </Col>
              </Row>
              <Table
                rowKey={record => record.index}
afc163's avatar
afc163 committed
436
                size="small"
437 438 439 440 441 442 443 444 445
                columns={columns}
                dataSource={searchData}
                pagination={{
                  style: { marginBottom: 0 },
                  pageSize: 5,
                }}
              />
            </Card>
          </Col>
446
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
447
            <Card
448
              loading={loading}
niko's avatar
niko committed
449
              className={styles.salesCard}
450 451
              bordered={false}
              title="销售额类别占比"
452
              bodyStyle={{ padding: 24 }}
niko's avatar
niko committed
453
              extra={
niko's avatar
niko committed
454 455 456 457 458 459 460 461 462 463
                <div className={styles.salesCardExtra}>
                  {iconGroup}
                  <div className={styles.salesTypeRadio}>
                    <Radio.Group value={salesType} onChange={this.handleChangeSalesType}>
                      <Radio.Button value="all">全部渠道</Radio.Button>
                      <Radio.Button value="online">线上</Radio.Button>
                      <Radio.Button value="offline">门店</Radio.Button>
                    </Radio.Group>
                  </div>
                </div>
niko's avatar
niko committed
464
              }
afc163's avatar
afc163 committed
465
              style={{ marginTop: 24, minHeight: 509 }}
466
            >
偏右's avatar
偏右 committed
467
              <h4 style={{ marginTop: 8, marginBottom: 32 }}>销售额</h4>
afc163's avatar
afc163 committed
468 469 470
              <Pie
                hasLegend
                subTitle="销售额"
jim's avatar
jim committed
471
                total={() => <Yuan>{salesPieData.reduce((pre, now) => now.y + pre, 0)}</Yuan>}
afc163's avatar
afc163 committed
472
                data={salesPieData}
jim's avatar
jim committed
473
                valueFormat={value => <Yuan>{value}</Yuan>}
afc163's avatar
afc163 committed
474 475 476
                height={248}
                lineWidth={4}
              />
477 478 479 480 481
            </Card>
          </Col>
        </Row>

        <Card
482
          loading={loading}
niko's avatar
niko committed
483
          className={styles.offlineCard}
484
          bordered={false}
niko's avatar
niko committed
485 486
          bodyStyle={{ padding: '0 0 32px 0' }}
          style={{ marginTop: 32 }}
487
        >
niko's avatar
niko committed
488 489 490 491 492 493 494 495 496 497 498 499
          <Tabs activeKey={activeKey} onChange={this.handleTabChange}>
            {offlineData.map(shop => (
              <TabPane tab={<CustomTab data={shop} currentTabKey={activeKey} />} key={shop.name}>
                <div style={{ padding: '0 24px' }}>
                  <TimelineChart
                    height={400}
                    data={offlineChartData}
                    titleMap={{ y1: '客流量', y2: '支付笔数' }}
                  />
                </div>
              </TabPane>
            ))}
500 501
          </Tabs>
        </Card>
jim's avatar
jim committed
502
      </GridContent>
503 504
    );
  }
afc163's avatar
afc163 committed
505
}