Analysis.js 15.2 KB
Newer Older
1 2
import React, { Component } from 'react';
import { connect } from 'dva';
afc163's avatar
afc163 committed
3
import { Row, Col, Icon, Card, Tabs, Table, Radio, DatePicker, Tooltip, Menu, Dropdown } from 'antd';
4
import numeral from 'numeral';
偏右's avatar
偏右 committed
5
import {
偏右's avatar
偏右 committed
6
  ChartCard, yuan, MiniArea, MiniBar, MiniProgress, Field, Bar, Pie, TimelineChart,
偏右's avatar
偏右 committed
7 8
} from '../../components/Charts';
import Trend from '../../components/Trend';
偏右's avatar
偏右 committed
9
import NumberInfo from '../../components/NumberInfo';
10 11 12 13
import { getTimeDistance } from '../../utils/utils';

import styles from './Analysis.less';

afc163's avatar
afc163 committed
14
const { TabPane } = Tabs;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
const { RangePicker } = DatePicker;

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

@connect(state => ({
  chart: state.chart,
}))
export default class Analysis extends Component {
  state = {
30
    loading: true,
31 32 33 34 35 36 37 38
    salesType: 'all',
    currentTabKey: '',
    rangePickerValue: [],
  }

  componentDidMount() {
    this.props.dispatch({
      type: 'chart/fetch',
39 40 41 42
    }).then(() => {
      this.setState({
        loading: false,
      });
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    });
  }

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

  handleChangeSalesType = (e) => {
    this.setState({
      salesType: e.target.value,
    });
  }

  handleTabChange = (key) => {
    this.setState({
      currentTabKey: key,
    });
  }

  handleRangePickerChange = (rangePickerValue) => {
    this.setState({
      rangePickerValue,
    });
nikogu's avatar
nikogu committed
69 70 71 72

    this.props.dispatch({
      type: 'chart/fetchSalesData',
    });
73 74 75 76 77 78 79 80 81 82 83 84
  }

  selectDate = (type) => {
    this.setState({
      rangePickerValue: getTimeDistance(type),
    });

    this.props.dispatch({
      type: 'chart/fetchSalesData',
    });
  }

afc163's avatar
afc163 committed
85 86 87 88 89 90 91 92 93 94 95
  isActive(type) {
    const { rangePickerValue } = this.state;
    const value = getTimeDistance(type);
    if (!rangePickerValue[0] || !rangePickerValue[1]) {
      return;
    }
    if (rangePickerValue[0].isSame(value[0], 'day') && rangePickerValue[1].isSame(value[1], 'day')) {
      return styles.currentDate;
    }
  }

96
  render() {
97
    const { rangePickerValue, salesType, currentTabKey, loading } = this.state;
98 99 100
    const { chart } = this.props;
    const {
      visitData,
afc163's avatar
afc163 committed
101
      visitData2,
102 103 104 105 106 107 108
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
afc163's avatar
afc163 committed
109
    } = chart;
110 111 112 113 114 115

    const salesPieData = salesType === 'all' ?
      salesTypeData
      :
      (salesType === 'online' ? salesTypeDataOnline : salesTypeDataOffline);

afc163's avatar
afc163 committed
116 117 118 119 120 121 122
    const menu = (
      <Menu>
        <Menu.Item>操作一</Menu.Item>
        <Menu.Item>操作二</Menu.Item>
      </Menu>
    );

123 124
    const iconGroup = (
      <span className={styles.iconGroup}>
afc163's avatar
afc163 committed
125 126 127
        <Dropdown overlay={menu} placement="bottomRight">
          <Icon type="ellipsis" />
        </Dropdown>
128 129 130
      </span>
    );

afc163's avatar
afc163 committed
131 132 133
    const salesExtra = (
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
afc163's avatar
afc163 committed
134 135 136 137 138 139 140 141 142 143 144 145
          <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
146 147 148 149 150 151
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={this.handleRangePickerChange}
          style={{ width: 256 }}
        />
152
      </div>
afc163's avatar
afc163 committed
153
    );
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171

    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
172
        className: styles.alignRight,
173 174 175 176 177 178 179
      },
      {
        title: '周涨幅',
        dataIndex: 'range',
        key: 'range',
        sorter: (a, b) => a.range - b.range,
        render: (text, record) => (
偏右's avatar
偏右 committed
180 181 182
          <Trend flag={record.status === 1 ? 'down' : 'up'}>
            <span style={{ marginRight: 4 }}>{text}%</span>
          </Trend>
183
        ),
afc163's avatar
afc163 committed
184
        className: styles.alignRight,
185 186 187
      },
    ];

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

190
    const CustomTab = ({ data, currentTabKey: currentKey }) => (
niko's avatar
niko committed
191
      <Row gutter={8} style={{ width: 138, margin: '8px 0' }}>
192 193 194 195
        <Col span={12}>
          <NumberInfo
            title={data.name}
            subTitle="转化率"
偏右's avatar
偏右 committed
196 197
            gap={2}
            total={`${data.cvr * 100}%`}
198 199 200 201 202
            theme={(currentKey !== data.name) && 'light'}
          />
        </Col>
        <Col span={12} style={{ paddingTop: 36 }}>
          <Pie
nikogu's avatar
nikogu committed
203
            animate={false}
afc163's avatar
Fix pie  
afc163 committed
204
            color={(currentKey !== data.name) && '#BDE4FF'}
205 206 207 208 209 210 211 212 213 214 215 216 217
            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
218 219 220
      md: 12,
      lg: 12,
      xl: 6,
221 222 223 224 225 226 227 228
      style: { marginBottom: 24 },
    };

    return (
      <div>
        <Row gutter={24}>
          <Col {...topColResponsiveProps}>
            <ChartCard
229
              loading={loading}
230
              bordered={false}
niko's avatar
niko committed
231
              title="总销售额"
afc163's avatar
afc163 committed
232
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
233
              total={yuan(126560)}
afc163's avatar
afc163 committed
234
              footer={<Field label="日均销售额" value={`¥${numeral(12423).format('0,0')}`} />}
235 236
              contentHeight={46}
            >
afc163's avatar
afc163 committed
237 238 239 240 241 242
              <Trend flag="up" style={{ marginRight: 16 }}>
                周同比<span className={styles.trendText}>12%</span>
              </Trend>
              <Trend flag="down">
                日环比<span className={styles.trendText}>11%</span>
              </Trend>
243 244 245 246
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
247
              loading={loading}
248 249
              bordered={false}
              title="访问量"
afc163's avatar
afc163 committed
250
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
251 252 253 254 255
              total={numeral(8846).format('0,0')}
              footer={<Field label="日访问量" value={numeral(1234).format('0,0')} />}
              contentHeight={46}
            >
              <MiniArea
niko's avatar
niko committed
256
                color="#975FE4"
257 258 259 260 261 262 263
                height={46}
                data={visitData}
              />
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
264
              loading={loading}
265 266
              bordered={false}
              title="支付笔数"
afc163's avatar
afc163 committed
267
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
268 269 270 271 272 273 274 275 276 277 278 279
              total={numeral(6560).format('0,0')}
              footer={<Field label="转化率" value="60%" />}
              contentHeight={46}
            >
              <MiniBar
                height={46}
                data={visitData}
              />
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
280
              loading={loading}
281
              bordered={false}
niko's avatar
niko committed
282
              title="运营活动效果"
afc163's avatar
afc163 committed
283
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
284
              total="78%"
afc163's avatar
afc163 committed
285
              footer={
afc163's avatar
afc163 committed
286
                <div style={{ whiteSpace: 'nowrap', overflow: 'hidden' }}>
afc163's avatar
afc163 committed
287 288 289 290 291 292
                  <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
293
                </div>
afc163's avatar
afc163 committed
294
              }
295 296
              contentHeight={46}
            >
niko's avatar
niko committed
297
              <MiniProgress percent={78} strokeWidth={8} target={80} color="#13C2C2" />
298 299 300 301 302
            </ChartCard>
          </Col>
        </Row>

        <Card
303
          loading={loading}
304 305 306 307
          bordered={false}
          bodyStyle={{ padding: 0 }}
        >
          <div className={styles.salesCard}>
afc163's avatar
afc163 committed
308
            <Tabs tabBarExtraContent={salesExtra} size="large" tabBarStyle={{ marginBottom: 24 }}>
309
              <TabPane tab="销售额" key="sales">
niko's avatar
niko committed
310
                <Row>
nikogu's avatar
nikogu committed
311
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
312 313
                    <div className={styles.salesBar}>
                      <Bar
niko's avatar
niko committed
314
                        height={295}
315 316 317 318
                        title="销售额趋势"
                        data={salesData}
                      />
                    </div>
319
                  </Col>
nikogu's avatar
nikogu committed
320
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
321 322 323 324 325 326
                    <div className={styles.salesRank}>
                      <h4 className={styles.rankingTitle}>门店销售额排名</h4>
                      <ul className={styles.rankingList}>
                        {
                          rankingListData.map((item, i) => (
                            <li key={item.title}>
afc163's avatar
afc163 committed
327
                              <span className={(i < 3) ? styles.active : ''}>{i + 1}</span>
328 329 330 331 332 333 334
                              <span>{item.title}</span>
                              <span>{numeral(item.total).format('0,0')}</span>
                            </li>
                          ))
                        }
                      </ul>
                    </div>
335 336 337
                  </Col>
                </Row>
              </TabPane>
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
              <TabPane tab="访问量" key="views">
                <Row gutter={72}>
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
                    <div className={styles.salesBar}>
                      <Bar
                        height={292}
                        title="访问量趋势"
                        data={salesData}
                      />
                    </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}>
                        {
                          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>
                          ))
                        }
                      </ul>
                    </div>
                  </Col>
                </Row>
366 367 368 369 370 371
              </TabPane>
            </Tabs>
          </div>
        </Card>

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

        <Card
467
          loading={loading}
niko's avatar
niko committed
468
          className={styles.offlineCard}
469
          bordered={false}
niko's avatar
niko committed
470 471
          bodyStyle={{ padding: '0 0 32px 0' }}
          style={{ marginTop: 32 }}
472 473
        >
          <Tabs
nikogu's avatar
nikogu committed
474
            activeKey={activeKey}
475 476 477 478 479
            onChange={this.handleTabChange}
          >
            {
              offlineData.map(shop => (
                <TabPane
nikogu's avatar
nikogu committed
480
                  tab={<CustomTab data={shop} currentTabKey={activeKey} />}
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
                  key={shop.name}
                >
                  <div style={{ padding: '0 24px' }}>
                    <TimelineChart
                      data={offlineChartData}
                      titleMap={{ y1: '客流量', y2: '支付笔数' }}
                    />
                  </div>
                </TabPane>)
              )
            }
          </Tabs>
        </Card>
      </div>
    );
  }
afc163's avatar
afc163 committed
497
}