Qlik Community

Qlik Sense Integration, Extensions, & APIs

Discussion board where members can learn more about Integration, Extensions and API’s for Qlik Sense.

Highlighted
Partner
Partner

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!

1 Solution

Accepted Solutions
Partner
Partner

Re: Color by Dimension is unexpectedly changing my bar chart

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.

1 Reply
Partner
Partner

Re: Color by Dimension is unexpectedly changing my bar chart

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.