AmCharts: Clustered bar chart with sub-categories










0















I try to realize a clustered bar chart with sub-categories in amcharts (two levels in my x-axis). I couldn't find a way to do it.



Does anyone know how to do it ?



An example of my data and the graph I need is available in the following link :



https://www.european-datalab.com/data-clustered-bar-chart/










share|improve this question


























    0















    I try to realize a clustered bar chart with sub-categories in amcharts (two levels in my x-axis). I couldn't find a way to do it.



    Does anyone know how to do it ?



    An example of my data and the graph I need is available in the following link :



    https://www.european-datalab.com/data-clustered-bar-chart/










    share|improve this question
























      0












      0








      0








      I try to realize a clustered bar chart with sub-categories in amcharts (two levels in my x-axis). I couldn't find a way to do it.



      Does anyone know how to do it ?



      An example of my data and the graph I need is available in the following link :



      https://www.european-datalab.com/data-clustered-bar-chart/










      share|improve this question














      I try to realize a clustered bar chart with sub-categories in amcharts (two levels in my x-axis). I couldn't find a way to do it.



      Does anyone know how to do it ?



      An example of my data and the graph I need is available in the following link :



      https://www.european-datalab.com/data-clustered-bar-chart/







      chart.js bar-chart amcharts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 16:30









      David MargueritDavid Marguerit

      567




      567






















          1 Answer
          1






          active

          oldest

          votes


















          3














          The tricky part is the parent category name_fr on the graph. To do that, we need to use Guide(https://docs.amcharts.com/3/javascriptcharts/Guide) within the CategoryAxis.



          But in order to use Guide, each category needs an unique identifier as each guide (in your case, it's each name) needs a starting point field (called Category) and an ending point field (called ToCategory).



          Create ID for each data point



          Here I just concat the name and date field. If your data has an ID field, you can use that too.



          let data = [

          "category": "Allemagne-1/3/2005",
          "name_fr": "Allemagne",
          "date": "1/3/2005",
          "Décile 1": 11.91166848,
          "Décile 5": 6.663209907,
          "Décile 9": 3.912389412
          ,

          "category": "Allemagne-12/18/2017",
          "name_fr": "Allemagne",
          "date": "12/18/2017",
          "Décile 1": 12.08203299,
          "Décile 5": 6.181569343,
          "Décile 9": 3.380401158
          ,

          "category": "Espagne-1/3/2005",
          "name_fr": "Espagne",
          "date": "1/3/2005",
          "Décile 1": 18.16145046,
          "Décile 5": 8.049555152,
          "Décile 9": 4.02786022
          ,

          "category": "Espagne-12/18/2017",
          "name_fr": "Espagne",
          "date": "12/18/2017",
          "Décile 1": 22.27695636,
          "Décile 5": 8.698725621,
          "Décile 9": 4.224440949
          ,

          "category": "France-1/3/2005",
          "name_fr": "France",
          "date": "1/3/2005",
          "Décile 1": 11.29143493,
          "Décile 5": 6.365859777,
          "Décile 9": 3.476250813
          ,

          "category": "France-12/18/2017",
          "name_fr": "France",
          "date": "12/18/2017",
          "Décile 1": 11.46405229,
          "Décile 5": 6.355936042,
          "Décile 9": 3.441408741
          ,

          "category": "Italie-1/3/2005",
          "name_fr": "Italie",
          "date": "1/3/2005",
          "Décile 1": 16.86187094,
          "Décile 5": 7.798630041,
          "Décile 9": 4.017535647
          ,

          "category": "Italie-12/18/2017",
          "name_fr": "Italie",
          "date": "12/18/2017",
          "Décile 1": 21.92640815,
          "Décile 5": 9.365977512,
          "Décile 9": 4.893619709
          ,

          "category": "Royaume-Uni-1/3/2005",
          "name_fr": "Royaume-Uni",
          "date": "1/3/2005",
          "Décile 1": 13.55694413,
          "Décile 5": 6.402068504,
          "Décile 9": 3.057193284
          ,

          "category": "Royaume-Uni-12/19/2016",
          "name_fr": "Royaume-Uni",
          "date": "12/19/2016",
          "Décile 1": 13.19564289,
          "Décile 5": 6.639341135,
          "Décile 9": 3.359725023

          ];


          Create Guide array from data



          Before you plug the data into AmChart, if you don't want to hard code the Guide array, you can generate that from the data first.



          The idea here is to group by the data by name_fr, and then grab the first item category as the Category of the guide, and the last item category as the ToCategory of the guide.



          You can write your own JavaScript function to do group by, but here I am lazy and just want to use a library called underscore.js (https://underscorejs.org) to do so.



          let byName = _.groupBy(data, "name_fr");
          let guides = _.map(byName, function(items, key)
          return
          "category": _.first(items).category,
          "toCategory": _.last(items).category,
          "lineAlpha": 0,
          "expand": true,
          "label": key,
          "labelRotation": 0,
          "tickLength": 80
          ;
          );


          Make the graph



          Then you can feed the data along with the guides into AmChart to make the graph.



          enter image description here



          fiddle: http://jsfiddle.net/aq9Laaew/272466/






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323899%2famcharts-clustered-bar-chart-with-sub-categories%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3














            The tricky part is the parent category name_fr on the graph. To do that, we need to use Guide(https://docs.amcharts.com/3/javascriptcharts/Guide) within the CategoryAxis.



            But in order to use Guide, each category needs an unique identifier as each guide (in your case, it's each name) needs a starting point field (called Category) and an ending point field (called ToCategory).



            Create ID for each data point



            Here I just concat the name and date field. If your data has an ID field, you can use that too.



            let data = [

            "category": "Allemagne-1/3/2005",
            "name_fr": "Allemagne",
            "date": "1/3/2005",
            "Décile 1": 11.91166848,
            "Décile 5": 6.663209907,
            "Décile 9": 3.912389412
            ,

            "category": "Allemagne-12/18/2017",
            "name_fr": "Allemagne",
            "date": "12/18/2017",
            "Décile 1": 12.08203299,
            "Décile 5": 6.181569343,
            "Décile 9": 3.380401158
            ,

            "category": "Espagne-1/3/2005",
            "name_fr": "Espagne",
            "date": "1/3/2005",
            "Décile 1": 18.16145046,
            "Décile 5": 8.049555152,
            "Décile 9": 4.02786022
            ,

            "category": "Espagne-12/18/2017",
            "name_fr": "Espagne",
            "date": "12/18/2017",
            "Décile 1": 22.27695636,
            "Décile 5": 8.698725621,
            "Décile 9": 4.224440949
            ,

            "category": "France-1/3/2005",
            "name_fr": "France",
            "date": "1/3/2005",
            "Décile 1": 11.29143493,
            "Décile 5": 6.365859777,
            "Décile 9": 3.476250813
            ,

            "category": "France-12/18/2017",
            "name_fr": "France",
            "date": "12/18/2017",
            "Décile 1": 11.46405229,
            "Décile 5": 6.355936042,
            "Décile 9": 3.441408741
            ,

            "category": "Italie-1/3/2005",
            "name_fr": "Italie",
            "date": "1/3/2005",
            "Décile 1": 16.86187094,
            "Décile 5": 7.798630041,
            "Décile 9": 4.017535647
            ,

            "category": "Italie-12/18/2017",
            "name_fr": "Italie",
            "date": "12/18/2017",
            "Décile 1": 21.92640815,
            "Décile 5": 9.365977512,
            "Décile 9": 4.893619709
            ,

            "category": "Royaume-Uni-1/3/2005",
            "name_fr": "Royaume-Uni",
            "date": "1/3/2005",
            "Décile 1": 13.55694413,
            "Décile 5": 6.402068504,
            "Décile 9": 3.057193284
            ,

            "category": "Royaume-Uni-12/19/2016",
            "name_fr": "Royaume-Uni",
            "date": "12/19/2016",
            "Décile 1": 13.19564289,
            "Décile 5": 6.639341135,
            "Décile 9": 3.359725023

            ];


            Create Guide array from data



            Before you plug the data into AmChart, if you don't want to hard code the Guide array, you can generate that from the data first.



            The idea here is to group by the data by name_fr, and then grab the first item category as the Category of the guide, and the last item category as the ToCategory of the guide.



            You can write your own JavaScript function to do group by, but here I am lazy and just want to use a library called underscore.js (https://underscorejs.org) to do so.



            let byName = _.groupBy(data, "name_fr");
            let guides = _.map(byName, function(items, key)
            return
            "category": _.first(items).category,
            "toCategory": _.last(items).category,
            "lineAlpha": 0,
            "expand": true,
            "label": key,
            "labelRotation": 0,
            "tickLength": 80
            ;
            );


            Make the graph



            Then you can feed the data along with the guides into AmChart to make the graph.



            enter image description here



            fiddle: http://jsfiddle.net/aq9Laaew/272466/






            share|improve this answer



























              3














              The tricky part is the parent category name_fr on the graph. To do that, we need to use Guide(https://docs.amcharts.com/3/javascriptcharts/Guide) within the CategoryAxis.



              But in order to use Guide, each category needs an unique identifier as each guide (in your case, it's each name) needs a starting point field (called Category) and an ending point field (called ToCategory).



              Create ID for each data point



              Here I just concat the name and date field. If your data has an ID field, you can use that too.



              let data = [

              "category": "Allemagne-1/3/2005",
              "name_fr": "Allemagne",
              "date": "1/3/2005",
              "Décile 1": 11.91166848,
              "Décile 5": 6.663209907,
              "Décile 9": 3.912389412
              ,

              "category": "Allemagne-12/18/2017",
              "name_fr": "Allemagne",
              "date": "12/18/2017",
              "Décile 1": 12.08203299,
              "Décile 5": 6.181569343,
              "Décile 9": 3.380401158
              ,

              "category": "Espagne-1/3/2005",
              "name_fr": "Espagne",
              "date": "1/3/2005",
              "Décile 1": 18.16145046,
              "Décile 5": 8.049555152,
              "Décile 9": 4.02786022
              ,

              "category": "Espagne-12/18/2017",
              "name_fr": "Espagne",
              "date": "12/18/2017",
              "Décile 1": 22.27695636,
              "Décile 5": 8.698725621,
              "Décile 9": 4.224440949
              ,

              "category": "France-1/3/2005",
              "name_fr": "France",
              "date": "1/3/2005",
              "Décile 1": 11.29143493,
              "Décile 5": 6.365859777,
              "Décile 9": 3.476250813
              ,

              "category": "France-12/18/2017",
              "name_fr": "France",
              "date": "12/18/2017",
              "Décile 1": 11.46405229,
              "Décile 5": 6.355936042,
              "Décile 9": 3.441408741
              ,

              "category": "Italie-1/3/2005",
              "name_fr": "Italie",
              "date": "1/3/2005",
              "Décile 1": 16.86187094,
              "Décile 5": 7.798630041,
              "Décile 9": 4.017535647
              ,

              "category": "Italie-12/18/2017",
              "name_fr": "Italie",
              "date": "12/18/2017",
              "Décile 1": 21.92640815,
              "Décile 5": 9.365977512,
              "Décile 9": 4.893619709
              ,

              "category": "Royaume-Uni-1/3/2005",
              "name_fr": "Royaume-Uni",
              "date": "1/3/2005",
              "Décile 1": 13.55694413,
              "Décile 5": 6.402068504,
              "Décile 9": 3.057193284
              ,

              "category": "Royaume-Uni-12/19/2016",
              "name_fr": "Royaume-Uni",
              "date": "12/19/2016",
              "Décile 1": 13.19564289,
              "Décile 5": 6.639341135,
              "Décile 9": 3.359725023

              ];


              Create Guide array from data



              Before you plug the data into AmChart, if you don't want to hard code the Guide array, you can generate that from the data first.



              The idea here is to group by the data by name_fr, and then grab the first item category as the Category of the guide, and the last item category as the ToCategory of the guide.



              You can write your own JavaScript function to do group by, but here I am lazy and just want to use a library called underscore.js (https://underscorejs.org) to do so.



              let byName = _.groupBy(data, "name_fr");
              let guides = _.map(byName, function(items, key)
              return
              "category": _.first(items).category,
              "toCategory": _.last(items).category,
              "lineAlpha": 0,
              "expand": true,
              "label": key,
              "labelRotation": 0,
              "tickLength": 80
              ;
              );


              Make the graph



              Then you can feed the data along with the guides into AmChart to make the graph.



              enter image description here



              fiddle: http://jsfiddle.net/aq9Laaew/272466/






              share|improve this answer

























                3












                3








                3







                The tricky part is the parent category name_fr on the graph. To do that, we need to use Guide(https://docs.amcharts.com/3/javascriptcharts/Guide) within the CategoryAxis.



                But in order to use Guide, each category needs an unique identifier as each guide (in your case, it's each name) needs a starting point field (called Category) and an ending point field (called ToCategory).



                Create ID for each data point



                Here I just concat the name and date field. If your data has an ID field, you can use that too.



                let data = [

                "category": "Allemagne-1/3/2005",
                "name_fr": "Allemagne",
                "date": "1/3/2005",
                "Décile 1": 11.91166848,
                "Décile 5": 6.663209907,
                "Décile 9": 3.912389412
                ,

                "category": "Allemagne-12/18/2017",
                "name_fr": "Allemagne",
                "date": "12/18/2017",
                "Décile 1": 12.08203299,
                "Décile 5": 6.181569343,
                "Décile 9": 3.380401158
                ,

                "category": "Espagne-1/3/2005",
                "name_fr": "Espagne",
                "date": "1/3/2005",
                "Décile 1": 18.16145046,
                "Décile 5": 8.049555152,
                "Décile 9": 4.02786022
                ,

                "category": "Espagne-12/18/2017",
                "name_fr": "Espagne",
                "date": "12/18/2017",
                "Décile 1": 22.27695636,
                "Décile 5": 8.698725621,
                "Décile 9": 4.224440949
                ,

                "category": "France-1/3/2005",
                "name_fr": "France",
                "date": "1/3/2005",
                "Décile 1": 11.29143493,
                "Décile 5": 6.365859777,
                "Décile 9": 3.476250813
                ,

                "category": "France-12/18/2017",
                "name_fr": "France",
                "date": "12/18/2017",
                "Décile 1": 11.46405229,
                "Décile 5": 6.355936042,
                "Décile 9": 3.441408741
                ,

                "category": "Italie-1/3/2005",
                "name_fr": "Italie",
                "date": "1/3/2005",
                "Décile 1": 16.86187094,
                "Décile 5": 7.798630041,
                "Décile 9": 4.017535647
                ,

                "category": "Italie-12/18/2017",
                "name_fr": "Italie",
                "date": "12/18/2017",
                "Décile 1": 21.92640815,
                "Décile 5": 9.365977512,
                "Décile 9": 4.893619709
                ,

                "category": "Royaume-Uni-1/3/2005",
                "name_fr": "Royaume-Uni",
                "date": "1/3/2005",
                "Décile 1": 13.55694413,
                "Décile 5": 6.402068504,
                "Décile 9": 3.057193284
                ,

                "category": "Royaume-Uni-12/19/2016",
                "name_fr": "Royaume-Uni",
                "date": "12/19/2016",
                "Décile 1": 13.19564289,
                "Décile 5": 6.639341135,
                "Décile 9": 3.359725023

                ];


                Create Guide array from data



                Before you plug the data into AmChart, if you don't want to hard code the Guide array, you can generate that from the data first.



                The idea here is to group by the data by name_fr, and then grab the first item category as the Category of the guide, and the last item category as the ToCategory of the guide.



                You can write your own JavaScript function to do group by, but here I am lazy and just want to use a library called underscore.js (https://underscorejs.org) to do so.



                let byName = _.groupBy(data, "name_fr");
                let guides = _.map(byName, function(items, key)
                return
                "category": _.first(items).category,
                "toCategory": _.last(items).category,
                "lineAlpha": 0,
                "expand": true,
                "label": key,
                "labelRotation": 0,
                "tickLength": 80
                ;
                );


                Make the graph



                Then you can feed the data along with the guides into AmChart to make the graph.



                enter image description here



                fiddle: http://jsfiddle.net/aq9Laaew/272466/






                share|improve this answer













                The tricky part is the parent category name_fr on the graph. To do that, we need to use Guide(https://docs.amcharts.com/3/javascriptcharts/Guide) within the CategoryAxis.



                But in order to use Guide, each category needs an unique identifier as each guide (in your case, it's each name) needs a starting point field (called Category) and an ending point field (called ToCategory).



                Create ID for each data point



                Here I just concat the name and date field. If your data has an ID field, you can use that too.



                let data = [

                "category": "Allemagne-1/3/2005",
                "name_fr": "Allemagne",
                "date": "1/3/2005",
                "Décile 1": 11.91166848,
                "Décile 5": 6.663209907,
                "Décile 9": 3.912389412
                ,

                "category": "Allemagne-12/18/2017",
                "name_fr": "Allemagne",
                "date": "12/18/2017",
                "Décile 1": 12.08203299,
                "Décile 5": 6.181569343,
                "Décile 9": 3.380401158
                ,

                "category": "Espagne-1/3/2005",
                "name_fr": "Espagne",
                "date": "1/3/2005",
                "Décile 1": 18.16145046,
                "Décile 5": 8.049555152,
                "Décile 9": 4.02786022
                ,

                "category": "Espagne-12/18/2017",
                "name_fr": "Espagne",
                "date": "12/18/2017",
                "Décile 1": 22.27695636,
                "Décile 5": 8.698725621,
                "Décile 9": 4.224440949
                ,

                "category": "France-1/3/2005",
                "name_fr": "France",
                "date": "1/3/2005",
                "Décile 1": 11.29143493,
                "Décile 5": 6.365859777,
                "Décile 9": 3.476250813
                ,

                "category": "France-12/18/2017",
                "name_fr": "France",
                "date": "12/18/2017",
                "Décile 1": 11.46405229,
                "Décile 5": 6.355936042,
                "Décile 9": 3.441408741
                ,

                "category": "Italie-1/3/2005",
                "name_fr": "Italie",
                "date": "1/3/2005",
                "Décile 1": 16.86187094,
                "Décile 5": 7.798630041,
                "Décile 9": 4.017535647
                ,

                "category": "Italie-12/18/2017",
                "name_fr": "Italie",
                "date": "12/18/2017",
                "Décile 1": 21.92640815,
                "Décile 5": 9.365977512,
                "Décile 9": 4.893619709
                ,

                "category": "Royaume-Uni-1/3/2005",
                "name_fr": "Royaume-Uni",
                "date": "1/3/2005",
                "Décile 1": 13.55694413,
                "Décile 5": 6.402068504,
                "Décile 9": 3.057193284
                ,

                "category": "Royaume-Uni-12/19/2016",
                "name_fr": "Royaume-Uni",
                "date": "12/19/2016",
                "Décile 1": 13.19564289,
                "Décile 5": 6.639341135,
                "Décile 9": 3.359725023

                ];


                Create Guide array from data



                Before you plug the data into AmChart, if you don't want to hard code the Guide array, you can generate that from the data first.



                The idea here is to group by the data by name_fr, and then grab the first item category as the Category of the guide, and the last item category as the ToCategory of the guide.



                You can write your own JavaScript function to do group by, but here I am lazy and just want to use a library called underscore.js (https://underscorejs.org) to do so.



                let byName = _.groupBy(data, "name_fr");
                let guides = _.map(byName, function(items, key)
                return
                "category": _.first(items).category,
                "toCategory": _.last(items).category,
                "lineAlpha": 0,
                "expand": true,
                "label": key,
                "labelRotation": 0,
                "tickLength": 80
                ;
                );


                Make the graph



                Then you can feed the data along with the guides into AmChart to make the graph.



                enter image description here



                fiddle: http://jsfiddle.net/aq9Laaew/272466/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 23:45









                David LiangDavid Liang

                5,75811830




                5,75811830





























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323899%2famcharts-clustered-bar-chart-with-sub-categories%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Barbados

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3