Skip to content

Commit

Permalink
Resolved #228 - Disable sorting for Histogram charts
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv2013 committed Mar 13, 2024
1 parent 10e48d2 commit 3d58c00
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 136 deletions.
32 changes: 20 additions & 12 deletions examples/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var json = {
{ "type": "rating", "name": "question1", "rateValues": [{ "value": 1, "text": "15 minutes" }, { "value": 2, "text": "30 minutes" }, { "value": 3, "text": "1 hour" }] },
{
"type": "text",
"name": "question1",
"name": "question2",
"inputType": "number"
}
]
Expand Down Expand Up @@ -132,24 +132,32 @@ var data = [
date: "2030-10-13",
age: 25
},
{ "question1": 3 }
{ "question1": 3 },
{ "question1": 1 },
{ "question1": 3 },
];

data = data.concat([
{
"question1": 15.1232432423
"question2": 15.1232432423
}, {
"question1": 32.1435232232
},
{
"question1": 14.1232432423
"question2": 32.1435232
}, {
"question1": 13.1435232232
},
{
"question1": 16.21000158
"question2": 14.1232432423
}, {
"question2": 13.1435232232
}, {
"question2": 16.21
}, {
"question2": 11.14352
}, {
"question2": 11.1435232232
}, {
"question2": 11.1435232232
}, {
"question2": 15
}, {
"question1": 11.1435232232
"question2": 44
},
]);

Expand Down
2 changes: 1 addition & 1 deletion examples/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var options = {
// hideEmptyAnswers: true,
// allowTopNAnswers: true,
// showCorrectAnswers: true
// labelTruncateLength: 27,
// labelTruncateLength: 27,
};

// SurveyAnalytics.WordCloudAdapter.drawOutOfBound = false;
Expand Down
45 changes: 23 additions & 22 deletions src/histogram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,19 +112,11 @@ export class HistogramModel extends SelectBase {
}

public getValues(): Array<any> {
const continiousValues = this.getContiniousValues();
if (this.hasCustomIntervals || continiousValues.length > HistogramModel.UseIntervalsFrom || this.needUseRateValues) {
return this.intervals.map(interval => interval.start);
}
return continiousValues.map(value => value.original);
return this.intervals.map(interval => interval.start);
}

public getLabels(): Array<string> {
const continiousValues = this.getContiniousValues();
if (this.hasCustomIntervals || continiousValues.length > HistogramModel.UseIntervalsFrom || this.needUseRateValues) {
return this.intervals.map(interval => interval.label);
}
return continiousValues.map(value => "" + value.original);
return this.intervals.map(interval => interval.label);
}

public get hasCustomIntervals() {
Expand All @@ -133,16 +125,28 @@ export class HistogramModel extends SelectBase {

public get intervals() {
if (this.hasCustomIntervals) {
return this.questionOptions.intervals.reverse();
return this.questionOptions.intervals;
}

if (this.needUseRateValues) {
const rateValues = this.question["rateValues"] as ItemValue[];
return rateValues.map((rateValue, i) => ({
start: rateValue.value,
end: i < rateValues.length - 1 ? rateValues[i + 1].value : rateValue.value + 1,
label: rateValue.text
})).reverse();
if(this.question.getType() == "rating") {
if (this.needUseRateValues) {
const rateValues = this.question["rateValues"] as ItemValue[];
return rateValues.map((rateValue, i) => ({
start: rateValue.value,
end: i < rateValues.length - 1 ? rateValues[i + 1].value : rateValue.value + 1,
label: rateValue.text
}));
} else {
const rateIntervals = [];
for(let i = (this.question["rateMin"] || 0); i <= (this.question["rateMax"] || (HistogramModel.IntervalsCount - 1)); i += (this.question["rateStep"] || 1)) {
rateIntervals.push({
start: i,
end: i + 1,
label: "" + (!!this.question["rateMin"] && !!this.question["rateMax"] ? i : (i + "-" + (i+1)))
});
}
return rateIntervals;
}
}

if (this._cachedIntervals === undefined) {
Expand All @@ -159,7 +163,7 @@ export class HistogramModel extends SelectBase {
const inext = this.toPrecision(next);
this._cachedIntervals.push({
start: istart,
end: inext,
end: i < intervalsCount - 1 ? inext : inext + delta / 100,
label: "" + this.getString(istart) + "-" + this.getString(inext)
});
start = next;
Expand All @@ -171,9 +175,6 @@ export class HistogramModel extends SelectBase {

public getData() {
const continiousValues = this.getContiniousValues();
if (!this.hasCustomIntervals && continiousValues.length <= HistogramModel.UseIntervalsFrom) {
return super.getData();
}
const intervals = this.intervals;
const statistics: Array<Array<number>> = [];
const series = this.getSeriesValues();
Expand Down
12 changes: 7 additions & 5 deletions src/selectBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,7 @@ export class SelectBase
return null;
});
this.registerToolbarItem("changeAnswersOrder", () => {
if (
(this.options.allowChangeAnswersOrder === undefined ||
this.options.allowChangeAnswersOrder) &&
this.getSeriesValues().length === 0
) {
if (this.isSupportAnswersOrder()) {
this.choicesOrderSelector = DocumentHelper.createSelector(
[
{ text: localization.getString("defaultOrder"), value: "default" },
Expand Down Expand Up @@ -440,6 +436,12 @@ export class SelectBase
this.stateChanged("topN", value);
}

protected isSupportAnswersOrder(): boolean {
return (this.options.allowChangeAnswersOrder === undefined ||
this.options.allowChangeAnswersOrder) &&
this.getSeriesValues().length === 0;
}

protected isSupportMissingAnswers(): boolean {
return true;
}
Expand Down
Loading

0 comments on commit 3d58c00

Please sign in to comment.