2017-08-20 14:32:01 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-09-07 04:28:02 +08:00
|
|
|
import PollService from '/imports/ui/components/poll/service';
|
2019-05-25 00:18:55 +08:00
|
|
|
import { injectIntl } from 'react-intl';
|
2016-05-31 06:07:02 +08:00
|
|
|
|
2019-05-08 21:36:40 +08:00
|
|
|
class PollDrawComponent extends Component {
|
2016-05-31 06:07:02 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2016-06-22 07:19:39 +08:00
|
|
|
|
|
|
|
this.state = {
|
2020-05-08 02:33:37 +08:00
|
|
|
// We did it because it was calculated in the componentWillMount
|
|
|
|
calculated: false,
|
2017-07-27 20:35:55 +08:00
|
|
|
// flag indicating whether we need to continue calculating the sizes or display the annotation
|
2016-06-22 07:19:39 +08:00
|
|
|
prepareToDisplay: true,
|
2020-05-08 02:33:37 +08:00
|
|
|
// outer (white) rectangle's coordinates and sizes (calculated in componentDidMount)
|
2016-06-22 07:19:39 +08:00
|
|
|
outerRect: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
},
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// inner rectangle's coordinates and sizes
|
2016-06-22 07:19:39 +08:00
|
|
|
innerRect: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
width: 0,
|
|
|
|
height: 0,
|
|
|
|
},
|
|
|
|
thickness: 0,
|
|
|
|
backgroundColor: '#ffffff',
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// max line sizes
|
2016-06-22 07:19:39 +08:00
|
|
|
maxLineWidth: 0,
|
|
|
|
maxLineHeight: 0,
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// max widths of the keys (left) and percent (right) strings
|
2016-06-22 07:19:39 +08:00
|
|
|
maxLeftWidth: 0,
|
|
|
|
maxRightWidth: 0,
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// these parameters are used in calculations before and while displaying the final result
|
2016-06-22 07:19:39 +08:00
|
|
|
maxNumVotes: 0,
|
|
|
|
textArray: [],
|
|
|
|
maxDigitWidth: 0,
|
2016-06-23 06:20:08 +08:00
|
|
|
maxDigitHeight: 0,
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// start value used for font-size calculations
|
2016-06-22 07:19:39 +08:00
|
|
|
calcFontSize: 50,
|
|
|
|
currentLine: 0,
|
|
|
|
lineToMeasure: [],
|
2016-06-23 06:20:08 +08:00
|
|
|
fontSizeDirection: 1,
|
2016-06-22 07:19:39 +08:00
|
|
|
};
|
|
|
|
|
2020-05-08 02:33:37 +08:00
|
|
|
this.pollInitialCalculation = this.pollInitialCalculation.bind(this);
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2020-05-08 02:33:37 +08:00
|
|
|
this.pollInitialCalculation();
|
2016-06-22 07:19:39 +08:00
|
|
|
this.checkSizes();
|
|
|
|
}
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
// this might have to be changed if we want to reuse it for a presenter's poll popup
|
|
|
|
shouldComponentUpdate() {
|
2019-05-25 00:18:55 +08:00
|
|
|
const { prepareToDisplay } = this.state;
|
|
|
|
return prepareToDisplay === true;
|
2017-08-20 14:32:01 +08:00
|
|
|
}
|
|
|
|
|
2016-06-22 07:19:39 +08:00
|
|
|
componentDidUpdate() {
|
2019-05-25 00:18:55 +08:00
|
|
|
const { prepareToDisplay } = this.state;
|
|
|
|
if (prepareToDisplay) {
|
2016-06-22 07:19:39 +08:00
|
|
|
this.checkSizes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSizes() {
|
2019-05-25 00:18:55 +08:00
|
|
|
let { maxLineHeight } = this.state;
|
|
|
|
|
|
|
|
const {
|
|
|
|
currentLine,
|
|
|
|
maxLineWidth,
|
|
|
|
fontSizeDirection,
|
|
|
|
calcFontSize,
|
|
|
|
textArray,
|
2020-05-08 02:33:37 +08:00
|
|
|
calculated,
|
2019-05-25 00:18:55 +08:00
|
|
|
} = this.state;
|
|
|
|
|
|
|
|
const { annotation } = this.props;
|
2020-05-08 02:33:37 +08:00
|
|
|
if (!calculated) return null;
|
2020-04-02 02:29:27 +08:00
|
|
|
// increment the font size by 2 to prevent Maximum update depth exceeded
|
|
|
|
const fontSizeIncrement = 2;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// calculating the font size in this if / else block
|
2019-05-25 00:18:55 +08:00
|
|
|
if (fontSizeDirection !== 0) {
|
|
|
|
const key = `${annotation.id}_key_${currentLine}`;
|
|
|
|
const votes = `${annotation.id}_votes_${currentLine}`;
|
|
|
|
const percent = `${annotation.id}_percent_${currentLine}`;
|
2017-08-20 14:32:01 +08:00
|
|
|
const keySizes = this[key].getBBox();
|
|
|
|
const voteSizes = this[votes].getBBox();
|
|
|
|
const percSizes = this[percent].getBBox();
|
2017-06-03 03:25:02 +08:00
|
|
|
|
|
|
|
// first check if we can still increase the font-size
|
2019-05-25 00:18:55 +08:00
|
|
|
if (fontSizeDirection === 1) {
|
2019-05-22 22:44:17 +08:00
|
|
|
if (keySizes.width < maxLineWidth && keySizes.height < maxLineHeight
|
|
|
|
&& voteSizes.width < maxLineWidth && voteSizes.height < maxLineHeight
|
|
|
|
&& percSizes.width < maxLineWidth && percSizes.height < maxLineHeight) {
|
2016-06-22 07:19:39 +08:00
|
|
|
return this.setState({
|
2020-04-02 02:29:27 +08:00
|
|
|
calcFontSize: calcFontSize + fontSizeIncrement,
|
2016-06-22 07:19:39 +08:00
|
|
|
});
|
|
|
|
|
2017-07-07 20:18:54 +08:00
|
|
|
// we can't increase font-size anymore, start decreasing
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
return this.setState({
|
|
|
|
fontSizeDirection: -1,
|
2020-04-02 02:29:27 +08:00
|
|
|
calcFontSize: calcFontSize - fontSizeIncrement,
|
2017-06-03 03:25:02 +08:00
|
|
|
});
|
2019-05-25 00:18:55 +08:00
|
|
|
} if (fontSizeDirection === -1) {
|
2017-06-03 03:25:02 +08:00
|
|
|
// check if the font-size is still bigger than allowed
|
2019-05-22 22:44:17 +08:00
|
|
|
if (keySizes.width > maxLineWidth || keySizes.height > maxLineHeight
|
|
|
|
|| voteSizes.width > maxLineWidth || voteSizes.height > maxLineHeight
|
|
|
|
|| percSizes.width > maxLineWidth || percSizes.height > maxLineHeight) {
|
2016-06-22 07:19:39 +08:00
|
|
|
return this.setState({
|
2020-04-02 02:29:27 +08:00
|
|
|
calcFontSize: calcFontSize - fontSizeIncrement,
|
2016-06-22 07:19:39 +08:00
|
|
|
});
|
|
|
|
|
2017-07-07 20:18:54 +08:00
|
|
|
// font size is fine for the current line, switch to the next line
|
|
|
|
// or finish with the font-size calculations if this we are at the end of the array
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
2019-05-25 00:18:55 +08:00
|
|
|
if (currentLine < textArray.length - 1) {
|
2017-06-03 03:25:02 +08:00
|
|
|
return this.setState({
|
2019-05-25 00:18:55 +08:00
|
|
|
currentLine: currentLine + 1,
|
|
|
|
lineToMeasure: textArray[currentLine + 1],
|
2017-06-03 03:25:02 +08:00
|
|
|
});
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
return this.setState({
|
|
|
|
fontSizeDirection: 0,
|
|
|
|
currentLine: 0,
|
2019-05-25 00:18:55 +08:00
|
|
|
lineToMeasure: textArray[0],
|
2017-06-03 03:25:02 +08:00
|
|
|
});
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// next block is executed when we finally found a proper font size
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// finding the biggest width and height of the left and right strings,
|
|
|
|
// max real line height and max width value for 1 digit
|
2016-06-25 05:30:37 +08:00
|
|
|
let maxLeftWidth = 0;
|
|
|
|
let maxRightWidth = 0;
|
2016-06-22 07:19:39 +08:00
|
|
|
maxLineHeight = 0;
|
2019-05-25 00:18:55 +08:00
|
|
|
for (let i = 0; i < textArray.length; i += 1) {
|
|
|
|
const key = `${annotation.id}_key_${i}`;
|
|
|
|
const percent = `${annotation.id}_percent_${i}`;
|
2017-08-20 14:32:01 +08:00
|
|
|
const keySizes = this[key].getBBox();
|
|
|
|
const percSizes = this[percent].getBBox();
|
2016-06-22 07:19:39 +08:00
|
|
|
|
|
|
|
if (keySizes.width > maxLeftWidth) {
|
|
|
|
maxLeftWidth = keySizes.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (percSizes.width > maxRightWidth) {
|
|
|
|
maxRightWidth = percSizes.width;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keySizes.height > maxLineHeight) {
|
|
|
|
maxLineHeight = keySizes.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (percSizes.height > maxLineHeight) {
|
|
|
|
maxLineHeight = percSizes.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 00:18:55 +08:00
|
|
|
const digitRef = `${annotation.id}_digit`;
|
2017-08-20 14:32:01 +08:00
|
|
|
const maxDigitWidth = this[digitRef].getBBox().width;
|
|
|
|
const maxDigitHeight = this[digitRef].getBBox().height;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
return this.setState({
|
2017-06-03 03:25:02 +08:00
|
|
|
maxLeftWidth,
|
|
|
|
maxRightWidth,
|
|
|
|
maxLineHeight,
|
|
|
|
maxDigitWidth,
|
|
|
|
maxDigitHeight,
|
2016-06-22 07:19:39 +08:00
|
|
|
prepareToDisplay: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-08 02:33:37 +08:00
|
|
|
pollInitialCalculation() {
|
|
|
|
// in this part we retrieve the props and perform initial calculations for the state
|
|
|
|
// calculating only the parts which have to be done just once and don't require
|
|
|
|
// rendering / rerendering the text objects
|
|
|
|
|
|
|
|
// if (!state.initialState) return;
|
|
|
|
const { annotation } = this.props;
|
|
|
|
const { points, result } = annotation;
|
|
|
|
const { slideWidth, slideHeight, intl } = this.props;
|
|
|
|
|
|
|
|
// x1 and y1 - coordinates of the top left corner of the annotation
|
|
|
|
// initial width and height are the width and height of the annotation
|
|
|
|
// all the points are given as percentages of the slide
|
|
|
|
const x1 = points[0];
|
|
|
|
const y1 = points[1];
|
|
|
|
const initialWidth = points[2];
|
|
|
|
const initialHeight = points[3];
|
|
|
|
|
|
|
|
// calculating the data for the outer rectangle
|
|
|
|
// 0.001 is needed to accomodate bottom and right borders of the annotation
|
|
|
|
const x = (x1 / 100) * slideWidth;
|
|
|
|
const y = (y1 / 100) * slideHeight;
|
|
|
|
const width = ((initialWidth - 0.001) / 100) * slideWidth;
|
|
|
|
const height = ((initialHeight - 0.001) / 100) * slideHeight;
|
|
|
|
|
|
|
|
let votesTotal = 0;
|
|
|
|
let maxNumVotes = 0;
|
|
|
|
const textArray = [];
|
|
|
|
|
|
|
|
// counting the total number of votes, finding the biggest number of votes
|
|
|
|
result.reduce((previousValue, currentValue) => {
|
|
|
|
votesTotal = previousValue + currentValue.numVotes;
|
|
|
|
if (maxNumVotes < currentValue.numVotes) {
|
|
|
|
maxNumVotes = currentValue.numVotes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return votesTotal;
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
// filling the textArray with data to display
|
|
|
|
// adding value of the iterator to each line needed to create unique
|
|
|
|
// keys while rendering at the end
|
|
|
|
const arrayLength = result.length;
|
|
|
|
for (let i = 0; i < arrayLength; i += 1) {
|
|
|
|
const _tempArray = [];
|
|
|
|
const _result = result[i];
|
|
|
|
_tempArray.push(_result.key, `${_result.numVotes}`);
|
|
|
|
if (votesTotal === 0) {
|
|
|
|
_tempArray.push('0%');
|
|
|
|
_tempArray.push(i);
|
|
|
|
} else {
|
|
|
|
const percResult = (_result.numVotes / votesTotal) * 100;
|
|
|
|
_tempArray.push(`${Math.round(percResult)}%`);
|
|
|
|
_tempArray.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
textArray.push(_tempArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
// calculating the data for the inner rectangle
|
|
|
|
const innerWidth = width * 0.95;
|
|
|
|
const innerHeight = height - (width * 0.05);
|
|
|
|
const innerX = x + (width * 0.025);
|
|
|
|
const innerY = y + (width * 0.025);
|
|
|
|
const thickness = (width - innerWidth) / 10;
|
|
|
|
|
|
|
|
// calculating the maximum possible width and height of the each line
|
|
|
|
// 25% of the height goes to the padding
|
|
|
|
const maxLineWidth = innerWidth / 3;
|
|
|
|
const maxLineHeight = (innerHeight * 0.75) / textArray.length;
|
|
|
|
|
|
|
|
const lineToMeasure = textArray[0];
|
|
|
|
const { pollAnswerIds } = PollService;
|
|
|
|
const messageIndex = lineToMeasure[0].toLowerCase();
|
|
|
|
if (pollAnswerIds[messageIndex]) {
|
|
|
|
lineToMeasure[0] = intl.formatMessage(pollAnswerIds[messageIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
outerRect: {
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
},
|
|
|
|
innerRect: {
|
|
|
|
x: innerX,
|
|
|
|
y: innerY,
|
|
|
|
width: innerWidth,
|
|
|
|
height: innerHeight,
|
|
|
|
},
|
|
|
|
thickness,
|
|
|
|
maxNumVotes,
|
|
|
|
textArray,
|
|
|
|
maxLineWidth,
|
|
|
|
maxLineHeight,
|
|
|
|
lineToMeasure,
|
|
|
|
calculated: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-22 07:19:39 +08:00
|
|
|
renderPoll() {
|
2017-08-20 14:32:01 +08:00
|
|
|
const {
|
2019-05-25 00:18:55 +08:00
|
|
|
backgroundColor,
|
|
|
|
calcFontSize,
|
2017-08-20 14:32:01 +08:00
|
|
|
innerRect,
|
|
|
|
maxDigitHeight,
|
2019-05-25 00:18:55 +08:00
|
|
|
maxDigitWidth,
|
|
|
|
maxLeftWidth,
|
2017-08-20 14:32:01 +08:00
|
|
|
maxLineHeight,
|
2019-05-25 00:18:55 +08:00
|
|
|
maxNumVotes,
|
|
|
|
maxRightWidth,
|
|
|
|
outerRect,
|
|
|
|
textArray,
|
2017-08-20 14:32:01 +08:00
|
|
|
thickness,
|
|
|
|
} = this.state;
|
|
|
|
|
2019-05-23 02:00:44 +08:00
|
|
|
const { annotation, intl } = this.props;
|
|
|
|
|
2019-09-07 04:28:02 +08:00
|
|
|
const { pollAnswerIds } = PollService;
|
2017-08-20 14:32:01 +08:00
|
|
|
|
2019-06-26 20:03:34 +08:00
|
|
|
const isRTL = document.documentElement.getAttribute('dir') === 'rtl';
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
//* ********************************************************************************************
|
|
|
|
//* *****************************************MAGIC NUMBER***************************************
|
|
|
|
// There is no automatic vertical centering in SVG.
|
|
|
|
// To center the text element we have to move it down by the half of its height.
|
|
|
|
// But every text element has its own padding by default. The height we receive
|
|
|
|
// by calling getBBox() includes padding, but the anchor point doesn't consider it.
|
|
|
|
// This way the text element is moved down a little bit and we have to move it up a bit.
|
2016-06-23 06:20:08 +08:00
|
|
|
// 1/6 of the maximum height of the digit seems to work fine.
|
2017-06-03 03:25:02 +08:00
|
|
|
// Oleksandr Zhurbenko. June 22, 2016
|
2017-08-20 14:32:01 +08:00
|
|
|
const magicNumber = maxDigitHeight / 6;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// maximum height and width of the line bar
|
2017-08-20 14:32:01 +08:00
|
|
|
const maxBarWidth = ((innerRect.width * 0.9)
|
|
|
|
- maxLeftWidth) - maxRightWidth;
|
|
|
|
const barHeight = (innerRect.height * 0.75) / textArray.length;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Horizontal padding
|
2017-08-20 14:32:01 +08:00
|
|
|
const horizontalPadding = (innerRect.width * 0.1) / 4;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Vertical padding
|
2017-08-20 14:32:01 +08:00
|
|
|
const verticalPadding = (innerRect.height * 0.25) / (textArray.length + 1);
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Initial coordinates of the key column
|
2017-08-20 14:32:01 +08:00
|
|
|
let yLeft = ((innerRect.y + verticalPadding) + (barHeight / 2)) - magicNumber;
|
|
|
|
const xLeft = (innerRect.x + horizontalPadding) + 1;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Initial coordinates of the line bar column
|
2017-08-20 14:32:01 +08:00
|
|
|
const xBar = (innerRect.x + maxLeftWidth) + (horizontalPadding * 2);
|
|
|
|
let yBar = innerRect.y + verticalPadding;
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// Initial coordinates of the percentage column
|
2017-08-20 14:32:01 +08:00
|
|
|
let yRight = ((innerRect.y + verticalPadding) + (barHeight / 2)) - magicNumber;
|
2019-05-22 22:44:17 +08:00
|
|
|
const xRight = ((((innerRect.x + (horizontalPadding * 3))
|
|
|
|
+ maxLeftWidth) + maxRightWidth) + maxBarWidth + 1);
|
2017-08-20 14:32:01 +08:00
|
|
|
|
|
|
|
let yNumVotes = (innerRect.y + verticalPadding) - magicNumber;
|
2017-06-03 03:25:02 +08:00
|
|
|
const extendedTextArray = [];
|
2017-08-20 14:32:01 +08:00
|
|
|
for (let i = 0; i < textArray.length; i += 1) {
|
|
|
|
let barWidth;
|
|
|
|
if (maxNumVotes === 0 || annotation.result[i].numVotes === 0) {
|
2016-06-22 07:19:39 +08:00
|
|
|
barWidth = 1;
|
|
|
|
} else {
|
2017-08-20 14:32:01 +08:00
|
|
|
barWidth = (annotation.result[i].numVotes / maxNumVotes) * maxBarWidth;
|
2016-06-22 07:19:39 +08:00
|
|
|
}
|
|
|
|
|
2019-05-23 02:00:44 +08:00
|
|
|
let label = textArray[i][0];
|
|
|
|
const formattedMessageIndex = label.toLowerCase();
|
|
|
|
if (pollAnswerIds[formattedMessageIndex]) {
|
|
|
|
label = intl.formatMessage(pollAnswerIds[formattedMessageIndex]);
|
|
|
|
}
|
|
|
|
|
2017-06-03 03:25:02 +08:00
|
|
|
// coordinates and color of the text inside the line bar
|
|
|
|
// xNumVotesDefault and xNumVotesMovedRight are 2 different x coordinates for the text
|
|
|
|
// since if the line bar is too small then we place the number to the right of the bar
|
2017-08-20 14:32:01 +08:00
|
|
|
const xNumVotesDefault = (innerRect.x + maxLeftWidth) + (horizontalPadding * 2);
|
|
|
|
const xNumVotesMovedRight = (xNumVotesDefault + (barWidth / 2)) + (maxDigitWidth / 2);
|
2016-06-22 07:19:39 +08:00
|
|
|
|
2016-06-25 05:30:37 +08:00
|
|
|
let xNumVotes;
|
|
|
|
let color;
|
2017-08-20 14:32:01 +08:00
|
|
|
if (barWidth < maxDigitWidth + 8) {
|
2016-06-22 07:19:39 +08:00
|
|
|
xNumVotes = xNumVotesMovedRight;
|
|
|
|
color = '#333333';
|
|
|
|
} else {
|
|
|
|
xNumVotes = xNumVotesDefault;
|
|
|
|
color = 'white';
|
|
|
|
}
|
|
|
|
|
2019-05-22 22:44:17 +08:00
|
|
|
extendedTextArray[i] = {
|
2017-08-20 14:32:01 +08:00
|
|
|
key: `${annotation.id}_${textArray[i][3]}`,
|
2017-07-27 20:35:55 +08:00
|
|
|
keyColumn: {
|
2019-05-23 02:00:44 +08:00
|
|
|
keyString: label,
|
2017-07-27 20:35:55 +08:00
|
|
|
xLeft,
|
|
|
|
yLeft,
|
|
|
|
},
|
|
|
|
barColumn: {
|
2017-08-20 14:32:01 +08:00
|
|
|
votesString: textArray[i][1],
|
2017-07-27 20:35:55 +08:00
|
|
|
xBar,
|
|
|
|
yBar,
|
|
|
|
barWidth,
|
|
|
|
barHeight,
|
|
|
|
yNumVotes,
|
|
|
|
xNumVotes,
|
|
|
|
color,
|
2017-08-20 14:32:01 +08:00
|
|
|
numVotes: annotation.result[i].numVotes,
|
2017-07-27 20:35:55 +08:00
|
|
|
},
|
|
|
|
percentColumn: {
|
|
|
|
xRight,
|
|
|
|
yRight,
|
2017-08-20 14:32:01 +08:00
|
|
|
percentString: textArray[i][2],
|
2017-07-27 20:35:55 +08:00
|
|
|
},
|
|
|
|
};
|
2017-06-03 03:25:02 +08:00
|
|
|
|
|
|
|
// changing the Y coordinate for all the objects
|
2016-06-22 07:19:39 +08:00
|
|
|
yBar = yBar + barHeight + verticalPadding;
|
|
|
|
yLeft = yLeft + barHeight + verticalPadding;
|
|
|
|
yRight = yRight + barHeight + verticalPadding;
|
|
|
|
yNumVotes = yNumVotes + barHeight + verticalPadding;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<g>
|
|
|
|
<rect
|
2017-08-20 14:32:01 +08:00
|
|
|
x={outerRect.x}
|
|
|
|
y={outerRect.y}
|
|
|
|
width={outerRect.width}
|
|
|
|
height={outerRect.height}
|
2016-06-22 07:19:39 +08:00
|
|
|
strokeWidth="0"
|
2017-08-20 14:32:01 +08:00
|
|
|
fill={backgroundColor}
|
2016-06-22 07:19:39 +08:00
|
|
|
/>
|
|
|
|
<rect
|
2017-08-20 14:32:01 +08:00
|
|
|
x={innerRect.x}
|
|
|
|
y={innerRect.y}
|
|
|
|
width={innerRect.width}
|
|
|
|
height={innerRect.height}
|
2016-06-22 07:19:39 +08:00
|
|
|
stroke="#333333"
|
2017-08-20 14:32:01 +08:00
|
|
|
fill={backgroundColor}
|
|
|
|
strokeWidth={thickness}
|
2016-06-22 07:19:39 +08:00
|
|
|
/>
|
|
|
|
<text
|
2017-08-20 14:32:01 +08:00
|
|
|
x={innerRect.x}
|
|
|
|
y={innerRect.y}
|
2016-06-22 07:19:39 +08:00
|
|
|
fill="#333333"
|
|
|
|
fontFamily="Arial"
|
2017-08-20 14:32:01 +08:00
|
|
|
fontSize={calcFontSize}
|
2019-06-26 20:03:34 +08:00
|
|
|
textAnchor={isRTL ? 'end' : 'start'}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
2019-05-22 22:44:17 +08:00
|
|
|
{extendedTextArray.map(line => (
|
|
|
|
<tspan
|
2016-06-22 07:19:39 +08:00
|
|
|
x={line.keyColumn.xLeft}
|
|
|
|
y={line.keyColumn.yLeft}
|
2017-08-20 14:32:01 +08:00
|
|
|
dy={maxLineHeight / 2}
|
2016-06-25 05:30:37 +08:00
|
|
|
key={`${line.key}_key`}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
|
|
|
{line.keyColumn.keyString}
|
2019-05-22 22:44:17 +08:00
|
|
|
</tspan>
|
|
|
|
))}
|
2016-06-22 07:19:39 +08:00
|
|
|
</text>
|
2019-05-22 22:44:17 +08:00
|
|
|
{extendedTextArray.map(line => (
|
|
|
|
<rect
|
2017-06-03 03:25:02 +08:00
|
|
|
key={`${line.key}_bar`}
|
|
|
|
x={line.barColumn.xBar}
|
|
|
|
y={line.barColumn.yBar}
|
|
|
|
width={line.barColumn.barWidth}
|
|
|
|
height={line.barColumn.barHeight}
|
|
|
|
stroke="#333333"
|
|
|
|
fill="#333333"
|
2017-08-20 14:32:01 +08:00
|
|
|
strokeWidth={thickness - 1}
|
2019-05-22 22:44:17 +08:00
|
|
|
/>
|
|
|
|
))}
|
2016-06-22 07:19:39 +08:00
|
|
|
<text
|
2017-08-20 14:32:01 +08:00
|
|
|
x={innerRect.x}
|
|
|
|
y={innerRect.y}
|
2016-06-22 07:19:39 +08:00
|
|
|
fill="#333333"
|
|
|
|
fontFamily="Arial"
|
2017-08-20 14:32:01 +08:00
|
|
|
fontSize={calcFontSize}
|
2019-06-26 20:03:34 +08:00
|
|
|
textAnchor={isRTL ? 'start' : 'end'}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
2019-05-22 22:44:17 +08:00
|
|
|
{extendedTextArray.map(line => (
|
|
|
|
<tspan
|
2016-06-22 07:19:39 +08:00
|
|
|
x={line.percentColumn.xRight}
|
|
|
|
y={line.percentColumn.yRight}
|
2017-08-20 14:32:01 +08:00
|
|
|
dy={maxLineHeight / 2}
|
2016-06-25 05:30:37 +08:00
|
|
|
key={`${line.key}_percent`}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
|
|
|
{line.percentColumn.percentString}
|
2019-05-22 22:44:17 +08:00
|
|
|
</tspan>
|
|
|
|
))}
|
2016-06-22 07:19:39 +08:00
|
|
|
</text>
|
|
|
|
<text
|
2017-08-20 14:32:01 +08:00
|
|
|
x={innerRect.x}
|
|
|
|
y={innerRect.y}
|
2016-06-22 07:19:39 +08:00
|
|
|
fill="#333333"
|
|
|
|
fontFamily="Arial"
|
2017-08-20 14:32:01 +08:00
|
|
|
fontSize={calcFontSize}
|
2019-06-26 20:03:34 +08:00
|
|
|
textAnchor={isRTL ? 'end' : 'start'}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
2019-05-22 22:44:17 +08:00
|
|
|
{extendedTextArray.map(line => (
|
|
|
|
<tspan
|
2017-08-20 14:32:01 +08:00
|
|
|
x={line.barColumn.xNumVotes + (line.barColumn.barWidth / 2)}
|
|
|
|
y={line.barColumn.yNumVotes + (line.barColumn.barHeight / 2)}
|
|
|
|
dy={maxLineHeight / 2}
|
2017-07-07 20:18:37 +08:00
|
|
|
key={`${line.key}_numVotes`}
|
2016-06-22 07:19:39 +08:00
|
|
|
fill={line.barColumn.color}
|
|
|
|
>
|
|
|
|
{line.barColumn.numVotes}
|
2019-05-22 22:44:17 +08:00
|
|
|
</tspan>
|
|
|
|
))}
|
2016-06-22 07:19:39 +08:00
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderLine(line) {
|
2017-06-03 03:25:02 +08:00
|
|
|
// this func just renders the strings for one line
|
2019-05-25 00:18:55 +08:00
|
|
|
|
|
|
|
const { calcFontSize } = this.state;
|
|
|
|
const { annotation } = this.props;
|
|
|
|
|
2016-06-22 07:19:39 +08:00
|
|
|
return (
|
2019-05-25 00:18:55 +08:00
|
|
|
<g key={`${annotation.id}_line_${line[3]}`}>
|
2016-06-22 07:19:39 +08:00
|
|
|
<text
|
|
|
|
fontFamily="Arial"
|
2019-05-25 00:18:55 +08:00
|
|
|
fontSize={calcFontSize}
|
|
|
|
ref={(ref) => { this[`${annotation.id}_key_${line[3]}`] = ref; }}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
|
|
|
<tspan>
|
|
|
|
{line[0]}
|
|
|
|
</tspan>
|
|
|
|
</text>
|
|
|
|
<text
|
|
|
|
fontFamily="Arial"
|
2019-05-25 00:18:55 +08:00
|
|
|
fontSize={calcFontSize}
|
|
|
|
ref={(ref) => { this[`${annotation.id}_votes_${line[3]}`] = ref; }}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
|
|
|
<tspan>
|
|
|
|
{line[1]}
|
|
|
|
</tspan>
|
|
|
|
</text>
|
|
|
|
<text
|
|
|
|
fontFamily="Arial"
|
2019-05-25 00:18:55 +08:00
|
|
|
fontSize={calcFontSize}
|
|
|
|
ref={(ref) => { this[`${annotation.id}_percent_${line[3]}`] = ref; }}
|
2016-06-22 07:19:39 +08:00
|
|
|
>
|
|
|
|
<tspan>
|
|
|
|
{line[2]}
|
|
|
|
</tspan>
|
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderTestStrings() {
|
2019-05-25 00:18:55 +08:00
|
|
|
const { annotation } = this.props;
|
|
|
|
const {
|
|
|
|
calcFontSize,
|
|
|
|
fontSizeDirection,
|
|
|
|
lineToMeasure,
|
|
|
|
textArray,
|
|
|
|
} = this.state;
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
// check whether we need to render just one line, which means that
|
|
|
|
// we are still calculating the font-size
|
2017-06-03 03:25:02 +08:00
|
|
|
// or if we finished with the font-size and we need to render all the strings in order to
|
|
|
|
// determine the maxHeight, maxWidth and maxDigitWidth
|
2019-05-25 00:18:55 +08:00
|
|
|
if (fontSizeDirection !== 0) {
|
|
|
|
return this.renderLine(lineToMeasure);
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<g>
|
2019-05-25 00:28:10 +08:00
|
|
|
{textArray.map(line => this.renderLine(line))}
|
2017-06-03 03:25:02 +08:00
|
|
|
<text
|
|
|
|
fontFamily="Arial"
|
2019-05-25 00:18:55 +08:00
|
|
|
fontSize={calcFontSize}
|
|
|
|
ref={(ref) => { this[`${annotation.id}_digit`] = ref; }}
|
2017-06-03 03:25:02 +08:00
|
|
|
>
|
|
|
|
<tspan>
|
2017-07-07 20:18:54 +08:00
|
|
|
0
|
2017-06-08 04:29:35 +08:00
|
|
|
</tspan>
|
2017-06-03 03:25:02 +08:00
|
|
|
</text>
|
|
|
|
</g>
|
|
|
|
);
|
2016-05-31 06:07:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2019-05-25 00:18:55 +08:00
|
|
|
const { prepareToDisplay } = this.state;
|
2016-05-31 06:07:02 +08:00
|
|
|
return (
|
2016-06-22 07:19:39 +08:00
|
|
|
<g>
|
2019-05-25 00:18:55 +08:00
|
|
|
{prepareToDisplay
|
2019-05-22 22:44:17 +08:00
|
|
|
? this.renderTestStrings()
|
|
|
|
: this.renderPoll()
|
2016-06-23 06:20:08 +08:00
|
|
|
}
|
2016-06-22 07:19:39 +08:00
|
|
|
</g>
|
2016-05-31 06:07:02 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-08-20 14:32:01 +08:00
|
|
|
|
2019-05-08 21:36:40 +08:00
|
|
|
export default injectIntl(PollDrawComponent);
|
|
|
|
|
2017-08-20 14:32:01 +08:00
|
|
|
PollDrawComponent.propTypes = {
|
|
|
|
// Defines an annotation object, which contains all the basic info we need to draw a line
|
|
|
|
annotation: PropTypes.shape({
|
|
|
|
id: PropTypes.string.isRequired,
|
|
|
|
points: PropTypes.arrayOf(PropTypes.number).isRequired,
|
|
|
|
result: PropTypes.arrayOf(
|
|
|
|
PropTypes.shape({
|
|
|
|
id: PropTypes.number.isRequired,
|
|
|
|
key: PropTypes.string.isRequired,
|
|
|
|
numVotes: PropTypes.number.isRequired,
|
|
|
|
}),
|
|
|
|
).isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
// Defines the width of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideWidth: PropTypes.number.isRequired,
|
|
|
|
// Defines the height of the slide (svg coordinate system), which needed in calculations
|
|
|
|
slideHeight: PropTypes.number.isRequired,
|
|
|
|
};
|