Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8911827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:02:35+00:00 2026-06-15T04:02:35+00:00

In JavaScript, how do I transform a long table format to a data structure

  • 0

In JavaScript, how do I transform a “long table format” to a data structure suitable for a line chart:

var data = [
  {"type":"A"," year":2000," value":50},
  {"type":"A"," year":2001," value":51},
  {"type":"A"," year":2002," value":52},
  {"type":"B"," year":2000," value":60},
  {"type":"B"," year":2001," value":55},
  {"type":"B"," year":2002," value":57}
]

=>

var series = [ 
  {type: "A", values : [{x: 2000, y: 50}, {x: 2001, y: 52},] },
  {type: "B", values : [{x: 2000, y: 60}, {x: 2001, y: 55},] },
]

A vanilla JavaScript solution as well as a solution with the Crossfilter library – that can work on any dimension of the data – would be valuable:
https://github.com/square/crossfilter/wiki/API-Reference

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:02:36+00:00Added an answer on June 15, 2026 at 4:02 am

    Good question, but not staightforward ; )

    Have a look at https://github.com/NickQiZhu/dc.js/pull/91

    you’ll have something like :

    cr = crossfilter(data);
    typeDim=cr.dimension(function(d) {return d.type});
    typeGroup=typeDim.group();
    
    yearDim=cr.dimension(function(d) {return d.year});
    valDim=cr.dimension(function(d) {return d.value});
    
    yearTypeGroup = dateDimension.group();
    yearTypeGroup.reduce( 
        // add
        // pivot[0].idx()[i] returns the index of statusGroup
        // the reduce function hence construct a key-value object, keys being indexes of the pivot group. 
        function(p, v, i, pivot) { 
             ++p[pivot[0].idx()[i]].count;
            p[pivot[0].idx()[i]].value += +v.value;
            return p;
        },
        //remove
        function(p, v,i,pivot) {
            --p[pivot[0].idx()[i]].count;
            p[pivot[0].idx()[i]].value -= +v.value;
            return p;
        },
        //init
        function(pivot) {
            var l, i, obj ={};
            if(l = pivot[0].all().length){
                for(i=0; i<l; ++i) {
                    obj[i] = {count:0, value:0}
                }
            }
            return obj
        }
    );
    
    yearTypeGroup.pivot(typeGroup)
    

    Good luck ; )
    C.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

JavaScript/JQuery var arr=[]; $('.element').each(function(i) { arr.push({id:i,value:$(this).attr('data-value')}); }); $.get('/json.php?arr='+arr,function(result) { alert(result); }); PHP <?php $j
JavaScript: $('.addTable').live('click', function () { var appendTxt = <table id='tab1'> <tr><td><input type='text' name='input1' /></td><td><input
I have this Javascript data: [{id:123,type:test},{id:154,type:another}] How would you transform that into something so
javascript with> var customer=document.getElementById('custList').value; and that works... Why does it work BUT... var customer=(form1.custList.value);
I am using this javascript ( Super Table ) to transform my table in
I have this on a javascript var: (it's a http returned data, and I
For an iPad application I need to transform some CoffeeScript files into JavaScript files
javascript code: function getCheckbox(name,id){ var check = 0; var deger = option-checkbox-+id; var dom
I am using Javascript to load XML and transform it with XSLT. Everything works
I am trying to create some javascript links. I need to transform the ID

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.