Skip to main content
Announcements
Have questions about Qlik Connect? Join us live on April 10th, at 11 AM ET: SIGN UP NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
dselgo_eidex
Partner - Creator III
Partner - Creator III

Color by Dimension is unexpectedly changing my bar chart

Hello, I am currently working on a bar chart that will show the percent of students who achieved certain scores from year to year:

Dimensions: [Score], [Year]

Measures: =count({$} [Score]) / count({$} TOTAL<Year> [Score])

barchart_color_auto.PNG

 

Above is the expected view of the bar chart. But, when I change the chart to instead Color by Dimension, I get this:
barchart_color_byDimension.PNG

I typically set all of my charts to color by dimension, but this chart is the only one behaving this way. The reason I am posting this question in this forum is because I am using the qVis API to generate these charts in my web app. 

{
    "qLibraryId": "",
    "qDef": {
        "qGrouping": "N",
        "qFieldDefs": ["Score"],
        "qFieldLabels": ["Score"],
        "qSortCriterias": [
            {
                "qSortByState": 0,
                "qSortByFrequency": 0,
                "qSortByNumeric": 0,
                "qSortByAscii": 1,
                "qSortByLoadOrder": 1,
                "qSortByExpression": 1,
                "qExpression": {
                    "qv": "Score"
                },
                "qSortByGreyness": 0
            }
        ],
        "qNumberPresentations": [],
        "qReverseSort": false,
        "qActiveField": 0,
        "qLabelExpression": "",
        "autoSort": false,
        "cId": "rpbxTj",
        "othersLabel": "Others",
        "textAlign": {
            "auto": true,
            "align": "left"
        },
        "representation": {
            "type": "text",
            "urlLabel": ""
        }
    },
    "qNullSuppression": true,
    "qIncludeElemValue": false,
    "qOtherTotalSpec": {
        "qOtherMode": "OTHER_OFF",
        "qOtherCounted": {
            "qv": "10"
        },
        "qOtherLimit": {
            "qv": "0"
        },
        "qOtherLimitMode": "OTHER_GE_LIMIT",
        "qSuppressOther": false,
        "qForceBadValueKeeping": true,
        "qApplyEvenWhenPossiblyWrongResult": true,
        "qGlobalOtherGrouping": false,
        "qOtherCollapseInnerDimensions": false,
        "qOtherSortMode": "OTHER_SORT_DESCENDING",
        "qTotalMode": "TOTAL_OFF",
        "qReferencedExpression": {
            "qv": ""
        }
    },
    "qShowTotal": false,
    "qShowAll": false,
    "qOtherLabel": {
        "qv": "Others"
    },
    "qTotalLabel": {
        "qv": "Totals"
    },
    "qCalcCond": {
        "qv": ""
    },
    "qAttributeExpressions": [],
    "qAttributeDimensions": [
        {
            "qDef": "Score",
            "qLibraryId": "",
            "qSortBy": {
                "qSortByState": 0,
                "qSortByFrequency": 0,
                "qSortByNumeric": 0,
                "qSortByAscii": 1,
                "qSortByLoadOrder": 1,
                "qSortByExpression": 1,
                "qExpression": {
                    "qv": "Score"
                },
                "qSortByGreyness": 0
            },
            "qAttribute": true,
            "id": "colorByAlternative",
            "label": "Score"
        }
    ],
    "qCalcCondition": {
        "qCond": {
            "qv": ""
        },
        "qMsg": {
            "qv": ""
        }
    },
    "othersLabel": "Others"
}

 

{
    "qLibraryId": "",
    "qDef": {
        "qGrouping": "N",
        "qFieldDefs": ["Year"],
        "qFieldLabels": ["Year"],
        "qSortCriterias": [
            {
                "qSortByState": 0,
                "qSortByFrequency": 0,
                "qSortByNumeric": 1,
                "qSortByAscii": 1,
                "qSortByLoadOrder": 1,
                "qSortByExpression": 0,
                "qExpression": {
                    "qv": ""
                },
                "qSortByGreyness": 0
            }
        ],
        "qNumberPresentations": [],
        "qReverseSort": false,
        "qActiveField": 0,
        "qLabelExpression": "",
        "autoSort": true,
        "cId": "nvPJMJ",
        "othersLabel": "Others"
    },
    "qNullSuppression": true,
    "qIncludeElemValue": false,
    "qOtherTotalSpec": {
        "qOtherMode": "OTHER_OFF",
        "qOtherCounted": {
            "qv": "10"
        },
        "qOtherLimit": {
            "qv": "0"
        },
        "qOtherLimitMode": "OTHER_GE_LIMIT",
        "qSuppressOther": false,
        "qForceBadValueKeeping": true,
        "qApplyEvenWhenPossiblyWrongResult": true,
        "qGlobalOtherGrouping": false,
        "qOtherCollapseInnerDimensions": false,
        "qOtherSortMode": "OTHER_SORT_DESCENDING",
        "qTotalMode": "TOTAL_OFF",
        "qReferencedExpression": {
            "qv": ""
        }
    },
    "qShowTotal": false,
    "qShowAll": false,
    "qOtherLabel": {
        "qv": "Others"
    },
    "qTotalLabel": {
        "qv": "Totals"
    },
    "qCalcCond": {
        "qv": ""
    },
    "qAttributeExpressions": [],
    "qAttributeDimensions": [
        {
            "qDef": "Year",
            "qLibraryId": "",
            "qSortBy": {
                "qSortByState": 0,
                "qSortByFrequency": 0,
                "qSortByNumeric": 1,
                "qSortByAscii": 1,
                "qSortByLoadOrder": 1,
                "qSortByExpression": 0,
                "qExpression": {
                    "qv": ""
                },
                "qSortByGreyness": 0
            },
            "qAttribute": true,
            "id": "colorByAlternative",
            "label": "Year"
        }
    ],
    "qCalcCondition": {
        "qCond": {
            "qv": ""
        },
        "qMsg": {
            "qv": ""
        }
    }
}

 

It seems to me that the "qAttributeDimensions" is a part of the problem. I checked the properties in the two charts above and found that in the first (correct) image, "qAttributeDimensions" is []. In the second image, "qAttributeDimensions" has a value for the Year field. I expected this property change, but I don't understand why it has an affect on the chart like this.

Here is a basic idea of what the data model looks like:

barchart_color_dataModel.PNG

I would post an example, but in all honesty I tried creating one and could not replicate this same behavior with fake data. 

Before anyone suggests this, I have set my barchart "color" property to "auto": true, but I use applyPatches() in my app and this sets the "qDimensionAttributes" of my chart.

Any help would be appreciated!

Labels (5)
1 Solution

Accepted Solutions
dselgo_eidex
Partner - Creator III
Partner - Creator III
Author

So I finally found a solution to this, but I don't really understand why. For the record, this behavior also occurs if you are trying to Color by Expression.

It seems that you have to somehow reference the Dimension that you are coloring by somewhere in the expression outside of the TOTAL qualifier. I was able to get it to work with this:

=if(Not IsNull(STUDENT.YEAR), count({$} [Score]) / count({$} TOTAL<Year> [Score]))

You don't have to use Not IsNull, but that is what I figured would be an easy way to do it.

View solution in original post

1 Reply
dselgo_eidex
Partner - Creator III
Partner - Creator III
Author

So I finally found a solution to this, but I don't really understand why. For the record, this behavior also occurs if you are trying to Color by Expression.

It seems that you have to somehow reference the Dimension that you are coloring by somewhere in the expression outside of the TOTAL qualifier. I was able to get it to work with this:

=if(Not IsNull(STUDENT.YEAR), count({$} [Score]) / count({$} TOTAL<Year> [Score]))

You don't have to use Not IsNull, but that is what I figured would be an easy way to do it.