MyBlockRender.js 5.5 KB
Newer Older
1
import { Table, Popover } from 'antd';
chscls@163.com's avatar
chscls@163.com committed
2
import config from '@/webPublic/one_stop_public/config';
3 4
import table from '../assets/专家经验系统切图/智能报告/table.png';
import chart from '../assets/专家经验系统切图/智能报告/chart.png';
徐立's avatar
徐立 committed
5 6
import React from 'react';
import { connect } from 'dva';
7
const ReactHighcharts = config.ReactHighcharts;
徐立's avatar
徐立 committed
8 9

@connect(({ Report, loading }) => ({
10 11
	Report,
	loading: loading.models.Report,
徐立's avatar
徐立 committed
12 13
}))
class ChartComponent extends React.Component {
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	constructor(props) {
		super(props);
		this.state = {
			dataSource: [],
			columns: [],
			chartConfig: {},
		};
	}
	componentDidMount() {
		const { dispatch, data, userId } = this.props;

		dispatch({
			type: 'Report/getStatistics',
			payload: { ...data, studentId: userId },
			callback: (res) => {
				const { chartType, chartTitle } = data;
				if (chartType == '0') {
					this.setState({ ...res });
				} else {
					const cts = [];
					const series = [];
					const { columns, dataSource } = res;
					for (var i = 1; i < columns.length - 1; i++) {
						cts.push(columns[i].title);
					}

					for (var j = 0; j < dataSource.length - 1; j++) {
						const obj = dataSource[j];

						const ob = { name: obj[columns[0].dataIndex] };
						const dd = [];
						for (var i = 1; i < columns.length - 1; i++) {
							dd.push(obj[columns[i].dataIndex]);
						}
						ob.data = dd;
						series.push(ob);
					}
					const chartConfig = {
						chart: {
							type: chartType,
						},
						title: {
							text: chartTitle,
						},
						xAxis: {
							categories: cts,
						},
						tooltip: {
							pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>',
						},

						plotOptions: {
							line: {
								dataLabels: {
									// 开启数据标签
									enabled: true,
								},
								// 关闭鼠标跟踪,对应的提示框、点击事件会失效
								enableMouseTracking: false,
							},
						},
						series: series,
					};

					this.setState({ chartConfig });
				}
			},
		});
	}
	edit = () => {
		const { data, editKey, editBlock } = this.props;

		editBlock(true, editKey, data);
	};
	render() {
		const content = (
			<div>
				<a onClick={this.edit}>编辑</a>
			</div>
		);
		const { isView, data } = this.props;

		const { chartConfig, dataSource, columns } = this.state;
		const { chartType, chartTitle } = data;

		if (isView) {
			if (chartType == '0') {
				if (columns.length > 5) {
					columns[0].fixed = 'left';
					columns[0].width = 200;
					columns[0].title = chartTitle;
					for (var i = 1; i < columns.length; i++) {
						columns[i].width = 50;
					}
				}
				return (
					<div>
						<Table
							size="small"
							dataSource={dataSource}
							scroll={{ x: (columns.length - 1) * 50 + 200 }}
							columns={columns}
							pagination={false}
						/>
					</div>
				);
			} else {
				return <ReactHighcharts config={chartConfig} />;
			}
		} else {
			return (
				<Popover content={content}>
					<img src={chart} style={{ maxWidth: '100%' }} />
				</Popover>
			);
		}
	}
徐立's avatar
徐立 committed
131 132 133
}

@connect(({ Report, loading }) => ({
134 135
	Report,
	loading: loading.models.Report,
徐立's avatar
徐立 committed
136 137
}))
class TableComponent extends React.Component {
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
	constructor(props) {
		super(props);

		this.state = {
			dataSource: [],
			columns: [],
		};
	}
	componentDidMount() {
		const { dispatch, data, userId } = this.props;

		dispatch({
			type: 'Report/exportData',
			payload: { ...data, studentId: userId },
			callback: (res) => {
				this.setState({ ...res });
			},
		});
	}
	edit = () => {
		const { data, editKey, editBlock } = this.props;

		editBlock(false, editKey, data);
	};
	delete = () => {
		const { deleteBlock, editKey } = this.props;

		deleteBlock(editKey);
	};
	render() {
		const content = (
			<div>
				<a onClick={this.edit}>编辑</a>
			</div>
		);

		const { dataSource, columns } = this.state;
		const { isView } = this.props;

		return isView ? (
			<Table size="small" dataSource={dataSource} columns={columns} pagination={false} />
		) : (
			<Popover content={content}>
				<img src={table} style={{ maxWidth: '100%' }} />
			</Popover>
		);
	}
徐立's avatar
徐立 committed
185 186 187
}

const AtomicComponent = (props) => {
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	const { contentState, block } = props;
	const { isView, userId, editKey, editBlock, deleteBlock } = props.blockProps;

	const x = block.getEntityAt(0);
	if (x == null) {
		return <div />;
	}
	const entity = contentState.getEntity(x);

	const type = entity.getType();

	if (type === 'image') {
		const { src } = entity.getData();
		return (
			<div>
				<img src={config.httpServer + src} style={{ maxWidth: '100%' }} />
			</div>
		);
	}
	if (type === 'attr') {
		const { src, name } = entity.getData();
		return (
			<a href={config.httpServer + src} target="_blank">
				{name}
			</a>
		);
	} else if (type === 'video') {
		const { src } = entity.getData();
		return (
			<div>
				<video src={config.httpServer + src} controls="controls">
					您的浏览器不支持 video 标签。
				</video>
			</div>
		);
	} else if (type === 'table') {
		return <TableComponent data={entity.getData()} {...props.blockProps} />;
	} else if (type === 'chart') {
		return <ChartComponent data={entity.getData()} {...props.blockProps} />;
	}
};
export default function myBlockRenderer(isView, userId, editBlock, deleteBlock, contentBlock) {
	const type = contentBlock.getType();

	if (type == 'atomic') {
		return {
			component: AtomicComponent, // 指定组件
			editable: false, // 这里设置自定义的组件
			props: {
				isView: isView,
				userId: userId,
				editBlock: editBlock,
				deleteBlock: deleteBlock,
				editKey: contentBlock.getKey(),
			},
		};
	}
徐立's avatar
徐立 committed
245
}