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 8759115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:43:13+00:00 2026-06-13T14:43:13+00:00

so i have an already working jquery ajax application that i’m trying to create

  • 0

so i have an already working jquery ajax application that i’m trying to create graphs for using d3js. i’m pretty familiar with jq but this is my first project using d3.

so, my html file using jquery.ajax to request json encoded data from a php script/mysql db. all my data is returned in a format similar to this:

{"status":"Failed","msg":"Bad request."}

or if it’s data like this (this is an age breakdown of users):

{"status":"Found","msg":{"under_18":103,"18-23":841,"24-29":1436,"30-36":1058,"37-46":907,"47-56":483,"over_56":200}}

i already have login/session/cookie stuff working with jq. i can request data from my ajax api, format it, and draw it to the screen with no problem.

so im trying to use d3js to create a pie chart w/ the 2nd json blob i posted above. here’s a the snip of code i’m working on:

function ageDemographics() {
    closure(
        'action=ageDemographics',
        function(result) {
            var width = 200,
                height = 200,
                outerRadius = Math.min(width, height) / 2,
                innerRadius = outerRadius * .6,
                data = d3.values(result.msg),
                color = d3.scale.category20(),
                donut = d3.layout.pie(),
                arc = d3.svg.arc().innerRadius(0).outerRadius(outerRadius);
            var vis = d3.select("#ageDemographics")
              .append("svg")
                .data([data])
                .attr("width", width)
                .attr("height", height);
            var arcs = vis.selectAll("g.arc")
                .data(donut)
              .enter().append("g")
                .attr("class", "arc")
                .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
            arcs.append("path")
                .attr("fill", function(d, i) { return color(i); })
                .attr("d", arc);
            arcs.append("text")
                .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
                .attr("dy", ".35em")
                .attr("text-anchor", "middle")
                .attr("display", function(d) { return d.value > .15 ? null : "none"; })
                .text(function(d, i) { return d.value.toFixed(2); });
        },
        function(xhr, status, thrown) {
            setupLoginForm();
            $('#error').html('You have been logged out.<br class="clear"/><br/>');                      
        }
    );
}
function closure(data, success, error) {
    $.ajax({
        type: "POST",
        url: 'http://localhost/analytics/api/ajax.php',
        data: data,
        cache: false,
        success: success,
        error: error
    });
}

i’m using the pie example from the d3js git repo and the chart is rendering “ok”, but on line #9 in the above code i’m calling:

data = d3.values(result.msg),

to pull out the values for d3 to generate the pie chart. but the labels on the pie are the values, which i gave it, but i’d like to display the keys instead.

e.g. “under 18”, “18-23”, etc, etc…

is this possible?
on line #31 i’m setting the text with:

.text(function(d, i) { return d.value.toFixed(2); });

but in this case “d” is just the numeric value. i was thinking i could use “i” to look up the key in the original “result.msg” but since the keys are strings not integers (like an array) i’m not sure how to do this.

ok, so i solved my problems. see my answer below, here’s a new question,
is it possible to move the labels out side the pie? or on rollover?

  • 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-13T14:43:14+00:00Added an answer on June 13, 2026 at 2:43 pm

    i figured it out.

    to create the labels:

    labels = d3.keys(result.msg)
    

    then on line 31:

    .text(function(d, i) { return labels[i]; });
    

    i hacked on this code for a whole day, and as soon as i posted it here i figured it out. lol

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

Sidebar

Related Questions

I am trying to use a jquery in my application where i have already
I am working on a web page that is using jQuery. I have an
I have a database table using an enum. This is already working with hibernate
I'm working on building a mobile application for a client using jQuery Mobile. The
I'm working with some web services that have already been created and I need
I have some jQuery AJAX code that takes the contents of a text-area and
I have been working on a json decode issue (which I have already had
I'm working on a video codec for OMAP3430. I already have code written in
I'd like to get a tcp/ip connection working over the internet. I already have
I'm already new in C++CLI , although I have been working on VB and

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.