Analysis.js 14.6 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 58 59

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

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

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

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

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

    this.props.dispatch({
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
90
  };
91

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

    this.props.dispatch({
      type: 'chart/fetchSalesData',
    });
niko's avatar
niko committed
100
  };
101

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

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

niko's avatar
niko committed
131 132 133
    const salesPieData =
      salesType === 'all'
        ? salesTypeData
jim's avatar
jim committed
134 135 136
        : salesType === 'online'
          ? salesTypeDataOnline
          : salesTypeDataOffline;
137

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

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

afc163's avatar
afc163 committed
153 154 155
    const salesExtra = (
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
afc163's avatar
afc163 committed
156 157 158 159 160 161 162 163 164 165 166 167
          <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
168 169 170 171 172 173
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={this.handleRangePickerChange}
          style={{ width: 256 }}
        />
174
      </div>
afc163's avatar
afc163 committed
175
    );
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    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
194
        className: styles.alignRight,
195 196 197 198 199 200 201
      },
      {
        title: '周涨幅',
        dataIndex: 'range',
        key: 'range',
        sorter: (a, b) => a.range - b.range,
        render: (text, record) => (
偏右's avatar
偏右 committed
202 203 204
          <Trend flag={record.status === 1 ? 'down' : 'up'}>
            <span style={{ marginRight: 4 }}>{text}%</span>
          </Trend>
205
        ),
afc163's avatar
afc163 committed
206
        align: 'right',
207 208 209
      },
    ];

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

212
    const CustomTab = ({ data, currentTabKey: currentKey }) => (
niko's avatar
niko committed
213
      <Row gutter={8} style={{ width: 138, margin: '8px 0' }}>
214 215 216 217
        <Col span={12}>
          <NumberInfo
            title={data.name}
            subTitle="转化率"
偏右's avatar
偏右 committed
218 219
            gap={2}
            total={`${data.cvr * 100}%`}
niko's avatar
niko committed
220
            theme={currentKey !== data.name && 'light'}
221 222 223 224
          />
        </Col>
        <Col span={12} style={{ paddingTop: 36 }}>
          <Pie
nikogu's avatar
nikogu committed
225
            animate={false}
niko's avatar
niko committed
226
            color={currentKey !== data.name && '#BDE4FF'}
227 228 229 230 231 232 233 234 235 236 237 238 239
            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
240 241 242
      md: 12,
      lg: 12,
      xl: 6,
243 244 245 246
      style: { marginBottom: 24 },
    };

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

niko's avatar
niko committed
329
        <Card loading={loading} bordered={false} bodyStyle={{ padding: 0 }}>
330
          <div className={styles.salesCard}>
afc163's avatar
afc163 committed
331
            <Tabs tabBarExtraContent={salesExtra} size="large" tabBarStyle={{ marginBottom: 24 }}>
332
              <TabPane tab="销售额" key="sales">
niko's avatar
niko committed
333
                <Row>
nikogu's avatar
nikogu committed
334
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
335
                    <div className={styles.salesBar}>
nikogu's avatar
nikogu committed
336
                      <Bar height={295} title="销售额趋势" data={salesData} />
337
                    </div>
338
                  </Col>
nikogu's avatar
nikogu committed
339
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
340 341 342
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店销售额排名</h4>
                      <ul className={styles.rankingList}>
niko's avatar
niko committed
343 344 345 346 347 348 349
                        {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>
                        ))}
350 351
                      </ul>
                    </div>
352 353 354
                  </Col>
                </Row>
              </TabPane>
355
              <TabPane tab="访问量" key="views">
陈帅's avatar
陈帅 committed
356
                <Row>
357 358
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesBar}>
niko's avatar
niko committed
359
                      <Bar height={292} title="访问量趋势" data={salesData} />
360 361 362 363 364 365
                    </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
366 367
                        {rankingListData.map((item, i) => (
                          <li key={item.title}>
368
                            <span className={i < 3 ? styles.active : ''}>{i + 1}</span>
niko's avatar
niko committed
369 370 371 372
                            <span>{item.title}</span>
                            <span>{numeral(item.total).format('0,0')}</span>
                          </li>
                        ))}
373 374 375 376
                      </ul>
                    </div>
                  </Col>
                </Row>
377 378 379 380 381 382
              </TabPane>
            </Tabs>
          </div>
        </Card>

        <Row gutter={24}>
383
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
384
            <Card
385
              loading={loading}
386 387 388 389 390 391 392 393
              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
394 395 396 397 398 399 400 401
                    subTitle={
                      <span>
                        搜索用户数
                        <Tooltip title="指标文案">
                          <Icon style={{ marginLeft: 8 }} type="info-circle-o" />
                        </Tooltip>
                      </span>
                    }
偏右's avatar
偏右 committed
402
                    gap={8}
403 404 405 406
                    total={numeral(12321).format('0,0')}
                    status="up"
                    subTotal={17.1}
                  />
niko's avatar
niko committed
407
                  <MiniArea line height={45} data={visitData2} />
408 409 410 411 412 413 414
                </Col>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
                    subTitle="人均搜索次数"
                    total={2.7}
                    status="down"
                    subTotal={26.2}
偏右's avatar
偏右 committed
415
                    gap={8}
416
                  />
niko's avatar
niko committed
417
                  <MiniArea line height={45} data={visitData2} />
418 419 420 421
                </Col>
              </Row>
              <Table
                rowKey={record => record.index}
afc163's avatar
afc163 committed
422
                size="small"
423 424 425 426 427 428 429 430 431
                columns={columns}
                dataSource={searchData}
                pagination={{
                  style: { marginBottom: 0 },
                  pageSize: 5,
                }}
              />
            </Card>
          </Col>
432
          <Col xl={12} lg={24} md={24} sm={24} xs={24}>
433
            <Card
434
              loading={loading}
niko's avatar
niko committed
435
              className={styles.salesCard}
436 437
              bordered={false}
              title="销售额类别占比"
438
              bodyStyle={{ padding: 24 }}
niko's avatar
niko committed
439
              extra={
niko's avatar
niko committed
440 441 442 443 444 445 446 447 448 449
                <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
450
              }
afc163's avatar
afc163 committed
451
              style={{ marginTop: 24, minHeight: 509 }}
452
            >
偏右's avatar
偏右 committed
453
              <h4 style={{ marginTop: 8, marginBottom: 32 }}>销售额</h4>
afc163's avatar
afc163 committed
454 455 456
              <Pie
                hasLegend
                subTitle="销售额"
jim's avatar
jim committed
457
                total={() => <Yuan>{salesPieData.reduce((pre, now) => now.y + pre, 0)}</Yuan>}
afc163's avatar
afc163 committed
458
                data={salesPieData}
jim's avatar
jim committed
459
                valueFormat={value => <Yuan>{value}</Yuan>}
afc163's avatar
afc163 committed
460 461 462
                height={248}
                lineWidth={4}
              />
463 464 465 466 467
            </Card>
          </Col>
        </Row>

        <Card
468
          loading={loading}
niko's avatar
niko committed
469
          className={styles.offlineCard}
470
          bordered={false}
niko's avatar
niko committed
471 472
          bodyStyle={{ padding: '0 0 32px 0' }}
          style={{ marginTop: 32 }}
473
        >
niko's avatar
niko committed
474 475 476 477 478 479 480 481 482 483 484 485
          <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>
            ))}
486 487
          </Tabs>
        </Card>
jim's avatar
jim committed
488
      </GridContent>
489 490
    );
  }
afc163's avatar
afc163 committed
491
}