Analysis.js 13.4 KB
Newer Older
1 2 3 4 5
import React, { Component } from 'react';
import { connect } from 'dva';
import { Row, Col, Icon, Card, Tabs, Table, Radio, DatePicker, Tooltip } from 'antd';
import numeral from 'numeral';

niko's avatar
niko committed
6
import { ChartCard, Trend, yuan, MiniArea, MiniBar, MiniProgress, Field, Bar, Pie, NumberInfo, IconUp, IconDown, TimelineChart } from '../../components/Charts';
7 8 9 10 11

import { getTimeDistance } from '../../utils/utils';

import styles from './Analysis.less';

afc163's avatar
afc163 committed
12
const { TabPane } = Tabs;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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 = {
    salesType: 'all',
    currentTabKey: '',
    rangePickerValue: [],
  }

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

  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
62 63 64 65

    this.props.dispatch({
      type: 'chart/fetchSalesData',
    });
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  }

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

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

  render() {
    const { rangePickerValue, salesType, currentTabKey } = this.state;
    const { chart } = this.props;
    const {
      visitData,
afc163's avatar
afc163 committed
83
      visitData2,
84 85 86 87 88 89 90
      salesData,
      searchData,
      offlineData,
      offlineChartData,
      salesTypeData,
      salesTypeDataOnline,
      salesTypeDataOffline,
afc163's avatar
afc163 committed
91
    } = chart;
92 93 94 95 96 97 98 99 100 101 102 103

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

    const iconGroup = (
      <span className={styles.iconGroup}>
        <Icon type="camera-o" /><Icon type="export" /><Icon type="ellipsis" />
      </span>
    );

afc163's avatar
afc163 committed
104 105 106 107 108 109 110 111 112 113 114 115 116
    const salesExtra = (
      <div className={styles.salesExtraWrap}>
        <div className={styles.salesExtra}>
          <a onClick={() => this.selectDate('today')}>今日</a>
          <a onClick={() => this.selectDate('week')}>本周</a>
          <a onClick={() => this.selectDate('month')}>本月</a>
          <a onClick={() => this.selectDate('year')}>全年</a>
        </div>
        <RangePicker
          value={rangePickerValue}
          onChange={this.handleRangePickerChange}
          style={{ width: 256 }}
        />
117
      </div>
afc163's avatar
afc163 committed
118
    );
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    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,
      },
      {
        title: '周涨幅',
        dataIndex: 'range',
        key: 'range',
        sorter: (a, b) => a.range - b.range,
        render: (text, record) => (
          <span style={{ textAlign: 'right' }}>{text}% {record.status === 1 ? <IconDown /> : <IconUp />}</span>
        ),
      },
    ];

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

151
    const CustomTab = ({ data, currentTabKey: currentKey }) => (
niko's avatar
niko committed
152
      <Row gutter={8} style={{ width: 138, margin: '8px 0' }}>
153 154 155 156 157 158 159 160 161 162
        <Col span={12}>
          <NumberInfo
            title={data.name}
            subTitle="转化率"
            total={`${data.cvr * 100}%`}
            theme={(currentKey !== data.name) && 'light'}
          />
        </Col>
        <Col span={12} style={{ paddingTop: 36 }}>
          <Pie
nikogu's avatar
nikogu committed
163
            animate={false}
164 165 166 167 168 169 170 171 172 173 174 175 176 177
            color={(currentKey !== data.name) && '#99d5fd'}
            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
178 179 180
      md: 12,
      lg: 12,
      xl: 6,
181 182 183 184 185 186 187 188 189
      style: { marginBottom: 24 },
    };

    return (
      <div>
        <Row gutter={24}>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
niko's avatar
niko committed
190
              title="总销售额"
afc163's avatar
afc163 committed
191
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
192
              total={yuan(126560)}
afc163's avatar
afc163 committed
193
              footer={<Field label="日均销售额" value={`¥${numeral(12423).format('0,0')}`} />}
194 195
              contentHeight={46}
            >
niko's avatar
niko committed
196
              <Trend colorType="gray" mini={['xlg', 'md']}>
afc163's avatar
afc163 committed
197
                <Trend.Item title="周同比" flag="up">12%</Trend.Item>
198 199 200 201 202 203 204 205
                <Trend.Item title="日环比" flag="down">11%</Trend.Item>
              </Trend>
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
              title="访问量"
afc163's avatar
afc163 committed
206
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
207 208 209 210 211
              total={numeral(8846).format('0,0')}
              footer={<Field label="日访问量" value={numeral(1234).format('0,0')} />}
              contentHeight={46}
            >
              <MiniArea
niko's avatar
niko committed
212
                color="#975FE4"
213 214 215 216 217 218 219 220 221
                height={46}
                data={visitData}
              />
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
              title="支付笔数"
afc163's avatar
afc163 committed
222
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
223 224 225 226 227 228 229 230 231 232 233 234 235
              total={numeral(6560).format('0,0')}
              footer={<Field label="转化率" value="60%" />}
              contentHeight={46}
            >
              <MiniBar
                height={46}
                data={visitData}
              />
            </ChartCard>
          </Col>
          <Col {...topColResponsiveProps}>
            <ChartCard
              bordered={false}
niko's avatar
niko committed
236
              title="运营活动效果"
afc163's avatar
afc163 committed
237
              action={<Tooltip title="指标说明"><Icon type="info-circle-o" /></Tooltip>}
238
              total="78%"
afc163's avatar
afc163 committed
239
              footer={
niko's avatar
niko committed
240
                <Trend mini={['xlg', 'md']}>
afc163's avatar
afc163 committed
241 242 243 244
                  <Trend.Item title="周同比" flag="up">12.3%</Trend.Item>
                  <Trend.Item title="日环比" flag="down">11%</Trend.Item>
                </Trend>
              }
245 246
              contentHeight={46}
            >
niko's avatar
niko committed
247
              <MiniProgress percent={78} strokeWidth={8} target={80} color="#13C2C2" />
248 249 250 251 252 253 254 255 256
            </ChartCard>
          </Col>
        </Row>

        <Card
          bordered={false}
          bodyStyle={{ padding: 0 }}
        >
          <div className={styles.salesCard}>
afc163's avatar
afc163 committed
257
            <Tabs tabBarExtraContent={salesExtra} size="large" tabBarStyle={{ marginBottom: 24 }}>
258
              <TabPane tab="销售额" key="sales">
niko's avatar
niko committed
259
                <Row>
nikogu's avatar
nikogu committed
260
                  <Col xl={16} lg={12} md={12} sm={24} xs={24}>
261 262
                    <div className={styles.salesBar}>
                      <Bar
niko's avatar
niko committed
263
                        height={295}
264 265 266 267
                        title="销售额趋势"
                        data={salesData}
                      />
                    </div>
268
                  </Col>
nikogu's avatar
nikogu committed
269
                  <Col xl={8} lg={12} md={12} sm={24} xs={24}>
270 271 272 273 274 275 276 277 278 279 280 281 282 283
                    <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>
284 285 286
                  </Col>
                </Row>
              </TabPane>
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
              <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>
315 316 317 318 319 320
              </TabPane>
            </Tabs>
          </div>
        </Card>

        <Row gutter={24}>
nikogu's avatar
nikogu committed
321
          <Col lg={12} sm={24} xs={24}>
322 323 324 325 326 327 328 329 330
            <Card
              bordered={false}
              title="线上热门搜索"
              extra={iconGroup}
              style={{ marginTop: 24 }}
            >
              <Row gutter={68}>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
niko's avatar
niko committed
331
                    subTitle={<span>搜索用户数 <Icon style={{ marginLeft: 8 }} type="info-circle-o" /></span>}
332 333 334 335 336 337 338
                    total={numeral(12321).format('0,0')}
                    status="up"
                    subTotal={17.1}
                  />
                  <MiniArea
                    line
                    height={45}
afc163's avatar
afc163 committed
339
                    data={visitData2}
340 341 342 343 344 345 346 347 348 349 350 351
                  />
                </Col>
                <Col sm={12} xs={24} style={{ marginBottom: 24 }}>
                  <NumberInfo
                    subTitle="人均搜索次数"
                    total={2.7}
                    status="down"
                    subTotal={26.2}
                  />
                  <MiniArea
                    line
                    height={45}
afc163's avatar
afc163 committed
352
                    data={visitData2}
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
                  />
                </Col>
              </Row>
              <Table
                rowKey={record => record.index}
                size="middle"
                columns={columns}
                dataSource={searchData}
                pagination={{
                  style: { marginBottom: 0 },
                  showSizeChanger: true,
                  showQuickJumper: true,
                  pageSize: 5,
                }}
              />
            </Card>
          </Col>
nikogu's avatar
nikogu committed
370
          <Col lg={12} sm={24} xs={24}>
371
            <Card
niko's avatar
niko committed
372
              className={styles.salesCard}
373 374
              bordered={false}
              title="销售额类别占比"
niko's avatar
niko committed
375 376 377 378 379 380 381 382 383 384 385 386
              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>
              )}
387 388
              style={{ marginTop: 24 }}
            >
niko's avatar
niko committed
389
              <div style={{ marginTop: 32, marginBottom: 54 }}>
390 391 392 393 394 395 396
                <Pie
                  hasLegend
                  title="销售额"
                  subTitle="销售额"
                  total={yuan(salesPieData.reduce((pre, now) => now.y + pre, 0))}
                  data={salesPieData}
                  valueFormat={val => yuan(val)}
niko's avatar
niko committed
397
                  height={314}
398 399 400 401 402 403 404
                />
              </div>
            </Card>
          </Col>
        </Row>

        <Card
niko's avatar
niko committed
405
          className={styles.offlineCard}
406
          bordered={false}
niko's avatar
niko committed
407 408
          bodyStyle={{ padding: '0 0 32px 0' }}
          style={{ marginTop: 32 }}
409 410
        >
          <Tabs
nikogu's avatar
nikogu committed
411
            activeKey={activeKey}
412 413 414 415 416
            onChange={this.handleTabChange}
          >
            {
              offlineData.map(shop => (
                <TabPane
nikogu's avatar
nikogu committed
417
                  tab={<CustomTab data={shop} currentTabKey={activeKey} />}
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
                  key={shop.name}
                >
                  <div style={{ padding: '0 24px' }}>
                    <TimelineChart
                      data={offlineChartData}
                      titleMap={{ y1: '客流量', y2: '支付笔数' }}
                    />
                  </div>
                </TabPane>)
              )
            }
          </Tabs>
        </Card>
      </div>
    );
  }
afc163's avatar
afc163 committed
434
}