使用JSON数据创建树图(Highcharts)

时间:2016-02-26 17:15:38

标签: php json highcharts

我正在尝试将数据库中的数据引入此Highcharts树形图,我不知道如何格式化代码的JSON部分。当有一个明确的xAxis和yAxis时,它相对容易。我们使用php汇编程序从查询中获取所有数据。我使用正常工作的JSON直接从Highcharts柱形图中获取代码,我尝试自己更新它并且它一直告诉我$.getJSON位是一个意外的标识符。

$(function () {

// Drop-in fix:
Highcharts.seriesTypes.treemap.prototype.getExtremesFromAll = true;

$('#container').highcharts({
    colorAxis: {
        minColor: ' #000000',
        maxColor: '#ffffff'
    },
    tooltip: {
        backgroundColor: '#fffff',
        formatter: function () {
            return this.point.name + ":" + this.point.value;
        }
    }

$.getJSON("/charts/data/homepage-charts-data.php?<?php echo $_SERVER['QUERY_STRING']; ?>", function(json) {
         options.categories = json[0]['data'];
         options.series[0] = json[1];
         chart = new Highcharts.Chart(options);

    });

  });

});

格式化数据的代码(来自:/charts/data/homepage-charts-data.php)

function displayJsonHomepageBallotMeasuresDataChart ($aaQuery) {


        global $NIMSPv4;
        $result = $NIMSPv4->query("$aaQuery");

        $categories = array();
        $categories['name'] = 'Election_Jurisdiction';

        $series = array();
        $series['name'] = 'Total_$';

        while($row = $result->fetch_assoc()) {
            $categories['data'][] = $row['Election_Jurisdiction'];
            $series['data'][] = $row['Total_$'];  
        }

        $result = array();
        array_push($result,$categories);
        array_push($result,$series);

        print json_encode($result, JSON_NUMERIC_CHECK);

} // END CHART FUNCTION

更新:这是我试图从中提取数据的JSON。 Election_Jurisdiction和Total_ $就是我现在想要访问的内容,考虑到它是一张树形图,我也可以放下候选人和Office_Sought,我需要展示比赛。

{
"metaInfo": {
"format": "json",
"completeness": {
  "allReports": "297",
  "availableReports": "297",
  "completeReports": "300",
  "incompleteAvailable": "-3",
  "lastUpdated": "2015-11-04 09:23:28",
  "mostRecentReportDate": "2013-01-31"
},
"paging": {
  "pageLink": "p=0",
  "minPage": 0,
  "currentPage": 0,
  "maxPage": 0,
  "totalPages": 1,
  "totalRecords": "3",
  "recordsPerPage": 100,
  "recordsThisPage": 3
},
"grouping": {
  "groupLink": "gro=c-t-id",
  "currentGrouping": {
    "c-t-id": "Candidate (General)"
  },
  "availableGrouping": {
    "f-s": "Filing Jurisdiction (General)",
    "f-eid": "Filer (General)",
    "f-y": "Filing Year (Advanced)",
    "c-t-i": "Incumbency Data (Advanced)",
    "c-t-icod": "Incumbency Advantage (Advanced)",
    "d-nme": "Original Name (Advanced)",
    "d-amt": "Amount (Advanced)",
    "d-dte": "Date (Advanced)",
    "d-ludte": "Last Updated (Advanced)",
    "d-typ": "Type of Transaction (Advanced)",
    "d-llink": "Lobbying Entity? (Advanced)",
    "d-ad-str": "Street (Advanced)",
    "c-t-sts": "Status of Candidate (Candidates)",
    "d-id": "Record (Contributors)",
    "d-eid": "Contributor (Contributors)",
    "d-et": "Type of Contributor (Contributors)",
    "d-ccg": "Broad Sector (Contributors)",
    "d-cci": "General Industry (Contributors)",
    "d-ccb": "Specific Business (Contributors)",
    "d-ad-cty": "City (Contributors)",
    "d-ad-st": "State (Contributors)",
    "d-ad-zip": "Zip (Contributors)",
    "d-ins": "In-Jurisdiction (Contributors)",
    "d-empl": "Employer (Contributors)",
    "d-occupation": "Occupation (Contributors)"
  }
},
"sorting": {
  "sortLink": "so=u-tot&sod=0",
  "currentSorting": {
    "u-tot": "Total_$"
  },
  "sortingDirection": "descending",
  "availableSorting": {
    "c-t-id": "Candidate",
    "c-t-pt": "Specific_Party",
    "c-t-p": "General_Party",
    "s": "Election_Jurisdiction",
    "y": "Election_Year",
    "c-r-t": "Election_Type",
    "c-r-osid": "Office_Sought",
    "c-t-ftsts": "Election_Status",
    "c-t-ico": "Incumbency_Status",
    "u-rec": "#_of_Records"
  }
},
"recordFormat": {
  "request": "c-r-id=16739",
  "Candidate": {
    "token": "c-t-id",
    "id": "token value",
    "Candidate": "display value"
  },
  "Candidate_Entity": {
    "token": "c-t-eid",
    "id": "token value",
    "Candidate_Entity": "display value"
  },
  "Specific_Party": {
    "token": "c-t-pt",
    "id": "token value",
    "Specific_Party": "display value"
  },
  "General_Party": {
    "token": "c-t-p",
    "id": "token value",
    "General_Party": "display value"
  },
  "Election_Jurisdiction": {
    "token": "s",
    "id": "token value",
    "Election_Jurisdiction": "display value"
  },
  "Election_Year": {
    "token": "y",
    "id": "token value",
    "Election_Year": "display value"
  },
  "Election_Type": {
    "token": "c-r-t",
    "id": "token value",
    "Election_Type": "display value"
  },
  "Office_Sought": {
    "token": "c-r-osid",
    "id": "token value",
    "Office_Sought": "display value"
  },
  "Election_Status": {
    "token": "c-t-ftsts",
    "id": "token value",
    "Election_Status": "display value"
  },
  "Incumbency_Status": {
    "token": "c-t-ico",
    "id": "token value",
    "Incumbency_Status": "display value"
  },
  "#_of_Records": {
    "#_of_Records": "display value"
  },
  "Total_$": {
    "Total_$": "display value"
  }
}
},
"records": [
{
  "record_id": 1,
  "request": "c-r-id=16739&c-t-id=76674",
  "Candidate": {
    "token": "c-t-id",
    "id": "76674",
    "Candidate": "BUTLER, MACK N"
  },
  "Candidate_Entity": {
    "token": "c-t-eid",
    "id": "15881718",
    "Candidate_Entity": "BUTLER, MACK N"
  },
  "Specific_Party": {
    "token": "c-t-pt",
    "id": "139",
    "Specific_Party": "REPUBLICAN"
  },
  "General_Party": {
    "token": "c-t-p",
    "id": "2",
    "General_Party": "Republican"
  },
  "Election_Jurisdiction": {
    "token": "s",
    "id": "AL",
    "Election_Jurisdiction": "AL"
  },
  "Election_Year": {
    "token": "y",
    "id": "2012",
    "Election_Year": "2012"
  },
  "Election_Type": {
    "token": "c-r-t",
    "id": "3",
    "Election_Type": "Special"
  },
  "Office_Sought": {
    "token": "c-r-osid",
    "id": "143",
    "Office_Sought": "HOUSE DISTRICT 030"
  },
  "Election_Status": {
    "token": "c-t-ftsts",
    "id": "Won-General",
    "Election_Status": "Won-General"
  },
  "Incumbency_Status": {
    "token": "c-t-ico",
    "id": "O",
    "Incumbency_Status": "Open"
  },
  "#_of_Records": {
    "#_of_Records": "197"
  },
  "Total_$": {
    "Total_$": "94700.08"
  }
},
{
  "record_id": 2,
  "request": "c-r-id=16739&c-t-id=76438",
  "Candidate": {
    "token": "c-t-id",
    "id": "76438",
    "Candidate": "MCGLAUGHN, BETH"
  },
  "Candidate_Entity": {
    "token": "c-t-eid",
    "id": "15881720",
    "Candidate_Entity": "MCGLAUGHN, BETH"
  },
  "Specific_Party": {
    "token": "c-t-pt",
    "id": "33",
    "Specific_Party": "DEMOCRATIC"
  },
  "General_Party": {
    "token": "c-t-p",
    "id": "1",
    "General_Party": "Democratic"
  },
  "Election_Jurisdiction": {
    "token": "s",
    "id": "AL",
    "Election_Jurisdiction": "AL"
  },
  "Election_Year": {
    "token": "y",
    "id": "2012",
    "Election_Year": "2012"
  },
  "Election_Type": {
    "token": "c-r-t",
    "id": "3",
    "Election_Type": "Special"
  },
  "Office_Sought": {
    "token": "c-r-osid",
    "id": "143",
    "Office_Sought": "HOUSE DISTRICT 030"
  },
  "Election_Status": {
    "token": "c-t-ftsts",
    "id": "Lost-General",
    "Election_Status": "Lost-General"
  },
  "Incumbency_Status": {
    "token": "c-t-ico",
    "id": "O",
    "Incumbency_Status": "Open"
  },
  "#_of_Records": {
    "#_of_Records": "39"
  },
  "Total_$": {
    "Total_$": "22537.65"
  }
},
{
  "record_id": 3,
  "request": "c-r-id=16739&c-t-id=28927",
  "Candidate": {
    "token": "c-t-id",
    "id": "28927",
    "Candidate": "MCHUGH, ROB"
  },
  "Candidate_Entity": {
    "token": "c-t-eid",
    "id": "15881719",
    "Candidate_Entity": "MCHUGH, ROB"
  },
  "Specific_Party": {
    "token": "c-t-pt",
    "id": "139",
    "Specific_Party": "REPUBLICAN"
  },
  "General_Party": {
    "token": "c-t-p",
    "id": "2",
    "General_Party": "Republican"
  },
  "Election_Jurisdiction": {
    "token": "s",
    "id": "AL",
    "Election_Jurisdiction": "AL"
  },
  "Election_Year": {
    "token": "y",
    "id": "2012",
    "Election_Year": "2012"
  },
  "Election_Type": {
    "token": "c-r-t",
    "id": "3",
    "Election_Type": "Special"
  },
  "Office_Sought": {
    "token": "c-r-osid",
    "id": "143",
    "Office_Sought": "HOUSE DISTRICT 030"
  },
  "Election_Status": {
    "token": "c-t-ftsts",
    "id": "Lost-Primary",
    "Election_Status": "Lost-Primary"
  },
  "Incumbency_Status": {
    "token": "c-t-ico",
    "id": "O",
    "Incumbency_Status": "Open"
  },
  "#_of_Records": {
    "#_of_Records": "0"
  },
  "Total_$": {
    "Total_$": "0.00"
  }
}
]
}

0 个答案:

没有答案